Jump to content

UPDATE works, but inserts another row


araxius

Recommended Posts

I am trying to make a simple CMS, the table is created with this command(using php):

	private function buildDB() {
	$sql =
<<<MySQL_QUERY
		CREATE TABLE IF NOT EXISTS newcore (
			id 			MEDIUMINT NOT NULL AUTO_INCREMENT,
			title		VARCHAR(128),
			menutitle	VARCHAR(128),
			bodytext	TEXT,
			json		VARCHAR(1024),
			children		VARCHAR(128),
			template	INTEGER,
			created		VARCHAR(100),
			PRIMARY KEY (id)
		) ENGINE=MyISAM;
MySQL_QUERY;

	return mysql_query($sql);
}

 

everything works fine, insert, delete, select, etc. But when I update a row, the update is also done, but another row is created with the same values, but of course a new id. I do the update this way:

 

$created = time();
$sql = 'UPDATE newcore SET title="'.$title.'", menutitle="'.$menutitle.'", bodytext="'.$bodytext.
				'", json="'.$json.'", children="'.$children.'", template="'.$template.'", created="'.$created.'" WHERE id='.$req['id'];

mysql_query($sql);

 

All the values are entered by myself, so there is no possibility of problem in values to make it function like that. Please help me, It's driving me insane! Thanks in advance.

Link to comment
Share on other sites

Best guess is you have a logic error in your code and your INSERT query is also being executed when you execute the UPDATE query code or your browser is requesting your page twice and the second request is executing the code where your INSERT query is located.

 

It would be helpful if you posted the actual code on your page that is producing this symptom so that someone could see what your code actually is.

Link to comment
Share on other sites

Thanks but I have put echoes everywhere, so I would have noticed if it had happened. Here is the whole class code:

<?php
/*				title		VARCHAR(128),
			menutitle	VARCHAR(128),
			bodytext	TEXT,
			json		TEXT,
			children		VARCHAR(128),
			template	INTEGER,
			created		VARCHAR(100)
*/
class simpleCMS {

var $host;
var $username;
var $password;
var $db;

public function display_public($i) {
	if($i=="")
		//$q = "SELECT * FROM newcore ORDER BY created DESC LIMIT 3";
		$q = "SELECT * FROM newcore ORDER BY created ASC";
	else
		$q = "SELECT * FROM newcore WHERE id=".$i;
	$r = mysql_query($q);

	if ( $r !== false && mysql_num_rows($r) > 0 ) {
		while ( $a = mysql_fetch_assoc($r) ) {
			$id = stripslashes($a['id']);
			$title = stripslashes($a['title']);
			$menutitle = stripslashes($a['menutitle']);
			$bodytext = stripslashes($a['bodytext']);
			$json = stripslashes($a['json']);
			$children = stripslashes($a['children']);
			$template = stripslashes($a['template']);
			$created = stripslashes($a['created']);

			$entry_display .=
<<<ENTRY_DISPLAY

				<div class="post">
				<h2>
					$id - $menutitle : $title
				</h2>
				<p>
					BODY: $bodytext
				</p>
				<p>
					JSON: $json
				</p>
				<p>
					CHILDREN: $children
				</p>
				<p>
					TEMPLATE: $template
				</p>
				<p>
					TIME: $created
				</p>
				</div>

				<p class="admin_link">
				<a href="{$_SERVER['PHP_SELF']}?admin=1&alter=1&id={$id}">Alter This Entry</a>
				    
				<a href="{$_SERVER['PHP_SELF']}?admin=1&delete=1&id={$id}">Delete This Entry</a>
				</p>
ENTRY_DISPLAY;
		}
	}
	else {
		$entry_display =
<<<ENTRY_DISPLAY

			<h2> This Page Is Under Construction </h2>
			<p>
			No entries have been made on this page. 
			Please check back soon, or click the
			link below to add an entry!
			</p>

ENTRY_DISPLAY;
	}
	$entry_display .=
<<<ADMIN_OPTION
		<br><br><br>
		<p class="admin_link">
		<a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>
		</p>

ADMIN_OPTION;

	return $entry_display;
}

public function display_new_page() {
	return
<<<ADMIN_FORM

		<form action="{$_SERVER['PHP_SELF']}" method="post">

			<label for="title">Title:</label><br />
			<input name="title" id="title" type="text" maxlength="128" />
			<div class="clear"></div>

			<label for="menutitle">Menu Title:</label><br />
			<input name="menutitle" id="menutitle" type="text" maxlength="128" />
			<div class="clear"></div>

			<label for="bodytext">Body Text:</label><br />
			<textarea name="bodytext" id="bodytext"></textarea>
			<div class="clear"></div>

			<label for="json">JSON</label><br />
			<textarea name="json" id="json"></textarea>
			<div class="clear"></div>

			<label for="children">Children:</label><br />
			<input name="children" id="children" type="text" maxlength="128" />
			<div class="clear"></div>

			<label for="template">Template:</label><br />
			<input name="template" id="template" type="text" maxlength="128" />
			<div class="clear"></div>

			<input type="submit" value="Create This Entry!" />

		</form>

		<br />

		<a href="display.php">Back to Home</a>

ADMIN_FORM;
}

public function display_alter_page($i) {
	$q = "SELECT * FROM newcore WHERE id=".$i;

	$r = mysql_query($q);

	if ( $r !== false ) {
		$r=mysql_fetch_array($r);
		$a=$r;
		$id = stripslashes($a['id']);
		$title = stripslashes($a['title']);
		$menutitle = stripslashes($a['menutitle']);
		$bodytext = stripslashes($a['bodytext']);
		$json = stripslashes($a['json']);
		$children = stripslashes($a['children']);
		$template = stripslashes($a['template']);
		$created = stripslashes($a['created']);

		return
<<<ADMIN_FORM
			<form action="{$_SERVER['PHP_SELF']}?admin=1&alter=2&id={$i}" method="post">

				<label for="title">Title:</label><br />
				<input name="title" id="title" type="text" maxlength="128" value="{$title}"/>
				<div class="clear"></div>

				<label for="menutitle">Menu Title:</label><br />
				<input name="menutitle" id="menutitle" type="text" maxlength="128" value="{$menutitle}"/>
				<div class="clear"></div>

				<label for="bodytext">Body Text:</label><br />
				<textarea name="bodytext" id="bodytext">{$bodytext}</textarea>
				<div class="clear"></div>

				<label for="json">JSON</label><br />
				<textarea name="json" id="json">{$json}</textarea>
				<div class="clear"></div>

				<label for="children">Children:</label><br />
				<input name="children" id="children" type="text" maxlength="128"  value="{$children}"/>
				<div class="clear"></div>

				<label for="template">Template:</label><br />
				<input name="template" id="template" type="text" maxlength="128" value="{$template}" />
				<div class="clear"></div>

				<input type="submit" value="Alter This Entry!" />

			</form>

			<br />

			<a href="display.php">Back to Home</a>

ADMIN_FORM;
	}
	return "";
}

public function write($p) {
	if ( $_POST['title'] )
		$title = mysql_real_escape_string($_POST['title']);

	if ( $_POST['menutitle'] )
		$menutitle = mysql_real_escape_string($_POST['menutitle']);

	if ( $_POST['bodytext'])
		$bodytext = mysql_real_escape_string($_POST['bodytext']);

	if ( $_POST['json'])
		$json = mysql_real_escape_string($_POST['json']);
	else
		$json="{}";

	if ( $_POST['children'])
		$children = mysql_real_escape_string($_POST['children']);

	if ( $_POST['template'])
		$template = intval($_POST['template']);


	if ( $title ) {
		$created = time();
		$sql = "INSERT INTO newcore (title,menutitle,bodytext,json,children,template,created) VALUES ( '$title','$menutitle','$bodytext','$json','$children','$template','$created')";
		return mysql_query($sql);
	}
	else {
		return false;
	}
}

public function do_delete_page ($id) {
	$sql = "DELETE FROM newcore WHERE id=".$id;
	mysql_query($sql);

	return $this->display_public("");	
}

public function do_alter_page ($req) {
	if ( $req['title'] )
		$title = mysql_real_escape_string($req['title']);

	if ( $req['menutitle'] )
		$menutitle = mysql_real_escape_string($req['menutitle']);

	if ( $req['bodytext'])
		$bodytext = mysql_real_escape_string($req['bodytext']);

	if ( $req['json'])
		$json = mysql_real_escape_string($req['json']);
	else
		$json="{}";

	if ( $req['children'])
		$children = mysql_real_escape_string($req['children']);

	if ( $req['template'])
		$template = intval($req['template']);


	if ( $title  ) {
		$created = time();
		$sql = 'UPDATE newcore SET title="'.$title.'", menutitle="'.$menutitle.'", bodytext="'.$bodytext.
				'", json="'.$json.'", children="'.$children.'", template="'.$template.'", created="'.$created.'" WHERE id='.$req['id'];

		echo $sql;

		mysql_query($sql);

		return $this->display_public("");
	}
}


public function connect() {
	mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
	mysql_select_db($this->db) or die("Could not select database. " . mysql_error());

	return $this->buildDB();
}

private function buildDB() {
	$sql =
<<<MySQL_QUERY
		CREATE TABLE IF NOT EXISTS newcore (
			id 			MEDIUMINT NOT NULL AUTO_INCREMENT,
			title		VARCHAR(128),
			menutitle	VARCHAR(128),
			bodytext	TEXT,
			json		VARCHAR(1024),
			children		VARCHAR(128),
			template	INTEGER,
			created		VARCHAR(100),
			PRIMARY KEY (id)
		) ENGINE=MyISAM;
MySQL_QUERY;

	return mysql_query($sql);
}

public function buildMenu($id) {

}
}

?>

 

And here is where the class is used:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    
    <title>Simple CMS with PHP</title>
    
    <link rel="stylesheet" type="text/css" href="style.css" />
  </head>

  <body>
  	<div id="page-wrap">
    <?php
    
      include_once('_class/simpleCMS.php');
      $obj = new simpleCMS();

      $obj->host = 'localhost';
      $obj->username = '----';
      $obj->password = '----';
      $obj->db = '-----';
      $obj->connect();
    
      if ( $_POST )
        $obj->write($_POST);

if ($_GET['admin'] == 1 ) {
	if ($_REQUEST['alter'] == 1)  {
		echo $obj->display_alter_page($_REQUEST['id'] );
	}
	else if ($_REQUEST['alter'] == 2)  {
		echo $obj->do_alter_page($_REQUEST) ;
	}
	else if ($_REQUEST['delete'] == 1)  {
		echo $obj->do_delete_page($_REQUEST['id'] ) ;
	}
	else {
		echo $obj->display_new_page() ;
	}
}
else {
	echo $obj->display_public($_REQUEST['page']);
}
    
    ?>
</div>
  </body>

</html>

 

It was all the code. I only have removed the database information, which is not important in this case.

 

As you see, the source code is very simple. That why it is driving me insane! Thanks again.

Link to comment
Share on other sites

You are calling your ->write() method, which is where your INSERT query is at, every time you submit to the page -

      if ( $_POST )

        $obj->write($_POST);

 

When you submit the form from your display_alter_page() code, the above two lines of code inserts a record.

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.