Jump to content

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

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.