pippin418 Posted January 23, 2010 Share Posted January 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/189501-insert-problem/ Share on other sites More sharing options...
agrant Posted January 23, 2010 Share Posted January 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/189501-insert-problem/#findComment-1000319 Share on other sites More sharing options...
kickstart Posted January 23, 2010 Share Posted January 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/189501-insert-problem/#findComment-1000378 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.