Jump to content

INSERT problem


pippin418

Recommended Posts

So I have this bookmarks script I'm making and it works pretty well. The only problem is that once you try to add a second bookmark it erases all of them.

 

<?php
$name = $_POST['name'];
$url = $_POST['url'];
$desc = $_POST['desc'];
$uid = $_POST['uid'];
$con = mysql_connect('localhost','pippin','********');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db = mysql_select_db('pippin_bookmarks');
if(!$db) {
	die("Unable to select database");
}
$qry = "INSERT INTO bookmarks(uid, address, title, description) VALUES('$uid','$url','$name','$desc')";
$result = mysql_query($qry);

//Check whether the query was successful or not
if($result) {
	header("location: ub-index.php");
	exit();
} else {
	die("Query failed");
}

mysql_close($con);
?>

 

That's the script to add a bookmark. IT works to add one, but not two.

Link to comment
https://forums.phpfreaks.com/topic/189501-insert-problem/
Share on other sites

Is UID your primary key? Typically on a script like this, I don't use it in an insert statement, I'll use auto increment instead example of how to implement this mySQL feature is below:

 

CREATE TABLE `bookmarks` ( `uid` INT( 3 ) NOT NULL AUTO_INCREMENT, `name` VARCHAR( 100 ) NOT NULL , `url` VARCHAR( 250 ) NOT NULL , `description` VARCHAR( 500 ) NOT NULL , PRIMARY KEY ( `uid` ) );

 

More info... http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

 

Also are you using this script publicly? If so you might want to protect your script better again SQL injection attacks.

Link to comment
https://forums.phpfreaks.com/topic/189501-insert-problem/#findComment-1000319
Share on other sites

Hi

 

I agree with the above (although personally I tend to specify to name of the auto increment column in the insert but give it a value of NULL).

 

However the INSERT should not erase existing records. If a record already exists with the same unique key / index then it should error.

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/189501-insert-problem/#findComment-1000378
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.