...

View Full Version : PhpMySQL update tables



newGenWeb
08-09-2012, 03:26 AM
Hello everyone! I'm having a small issue with updating tables in MySQL.


As you see here in phpmyadmin: http://www.staciepreecedesigns.ca/simpsons/phpmyadmin.png

I have sid and id, the only problem is that they both need to be auto-increments, how can I do that? I know a few methods, but they can be a bit confusing, what is the best method for this?


The problem php code that I've narrowed down is (if there is a problem, I'm sure there's not, but just to make sure):



$idChar = $_GET['id'];
if(!isset($idChar)){//this value MUST be set in order for the next query to work


$x = mysql_query("SELECT sid FROM simpsons ORDER BY sid ASC LIMIT 1");
while($row = mysql_fetch_array($x)){
$idChar = $row['sid'];
}

}


$sUpdate = $_GET['update'];
if($sUpdate == "yes") {
//echo "<br />Form has been submitted<br />";

$newCharF = $_POST['fname'];
$newCharL = $_POST['lname'];
$newCharDes = $_POST['description'];
echo $newCharF . " | " . $newCharL . " | " . $newCharDes;


$sql = "UPDATE simpsons SET
fname=\"$newCharF\",
lname=\"$newCharL\",
description=\"$newCharDes\"
WHERE sid=$idChar";
//echo $idChar;

$results3 = mysql_query($sql) or die (mysql_error());
}

Inigoesdr
08-09-2012, 03:48 AM
Why would you want two auto increment fields or two fields doing the same thing at all for that matter? Seems silly, but to answer your question you could use a trigger to copy the value to the second field when a new row is created, or grab the auto_increment id with mysqli_insert_id() (http://php.net/mysql_insert_id) and copy the same value from the sid to id. Again, I can't see a reason to do this at all, but it's certainly possible.

newGenWeb
08-09-2012, 07:52 PM
I hate my school, but thanks for your input.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum