Jump to content

[SOLVED] mysql update doesn't work


vampke

Recommended Posts

Hi guys,

 

I'm trying to get this script to get data from a mysql table and put it in html.

So far so good.

Now I want to be able to update this data and put the changes back in the mysql table. Problems here.

The script I wrote for the update:

$update = "UPDATE aaarticle SET id='$_POST[id]', date='$_POST[date]', spec='$_POST[spec]', title='$_POST[title]', text='$_POST[text]' WHERE id= '$_POST[id]'";
mysql_query($update,$conn) or die(mysql_error());

 

This does not seem to be making any changes to the table though.

What could be wrong?

Link to comment
Share on other sites

try this:

$update = "UPDATE aaarticle SET id='$_POST['id']', date='$_POST['date']', spec='$_POST['spec']', title='$_POST['title']', text='$_POST['text']' WHERE id= '$_POST['id']'";
mysql_query($update,$conn) or die(mysql_error());

 

Link to comment
Share on other sites

ok For easy understanding:

 

$id='$_POST['id'];
$date='$_POST['date'];
$spec='$_POST['spec'];
$title='$_POST['title'];
$text='$_POST['text'];

$update = "UPDATE aaarticle SET id='$id', date='$date', spec='$spec', title='$title', text='$text' WHERE id= '$id'";
mysql_query($update,$conn) or die(mysql_error());

 

Link to comment
Share on other sites

Try this:

 

<?PHP
//make sure these variables are defined in $conn and that you have selected a database. 
$hostname = 'localhost';
$dbuser = 'dbuser';
$dbpass = 'dbpass';
$dbselect = 'dbname';

$conn = mysql_connect($hostname, $dbuser, $dbpass);
//lets make sure were connected with $conn - then select our database
if ($conn) { //if there were no connection issues
mysql_select_db($dbselect); //select the database
//ok now it looks like your trying to update the ID - and if I was a betting man - the id is auto_increment, which would cause this query to fail
// so we replace this: $update = "UPDATE aaarticle SET id='$_POST[id]', date='$_POST[date]', spec='$_POST[spec]', title='$_POST[title]', text='$_POST[text]' WHERE id= '$_POST[id]'";
// with 
$update = "UPDATE aaarticle SET date='$_POST[date]', spec='$_POST[spec]', title='$_POST[title]', text='$_POST[text]' WHERE id= '$_POST[id]'";
$result = mysql_query($update); // run the query
if ($result) { //if there was a result
  //do something
} else { //the query failed
	echo mysql_error();
}
} else { //connection issue
echo 'there was an issue connecting to the server, check that you have properly identified the credentials.';
}
?>

Link to comment
Share on other sites

ok For easy understanding:

 

$id='$_POST['id'];
$date='$_POST['date'];
$spec='$_POST['spec'];
$title='$_POST['title'];
$text='$_POST['text'];

$update = "UPDATE aaarticle SET id='$id', date='$date', spec='$spec', title='$title', text='$text' WHERE id= '$id'";
mysql_query($update,$conn) or die(mysql_error());

 

 

Parse error: syntax error, unexpected T_STRING in pathto/update.php on line 6

 

line 6 =

$id='$_POST['id']';

 

I added a ' after each line before ;

I think you forgot that in your reply, right?

Link to comment
Share on other sites

 

try this:

 

$id=$_POST['id'];
$date=$_POST['date'];
$spec=$_POST['spec'];
$title=$_POST['title'];
$text=$_POST['text'];

$update = "UPDATE aaarticle SET id='$id', date='$date', spec='$spec', title='$title', text='$text' WHERE id= '$id'";
mysql_query($update,$conn) or die(mysql_error());

 

Sorry for that.

Link to comment
Share on other sites

no

 

I understand your defining those but with what you just posted your going to end up with errors

 

you do not need to enclose globals in single quotes (') to define them - so they should be

 

$id = $_POST['id'];

 

and NOT

 

$id = '$_POST['id']; //will cause the error Parse error: syntax error, unexpected T_STRING in path/to/blah.php on line whatever

 

 

 

Link to comment
Share on other sites

I think your main problem is that you were trying to update the id - when there is no need to update the id - that should remain as it is-  the contents for that id are the only things that should change.

 

You can do it either way thought,

 

$update = "UPDATE article SET date='$_POST[date]'"; //this way works as long as its written as $_POST[date] and not $_POST['date']

 

or

 

$update = "UPDATE article SET date='$date'";

 

Its all in how you prefer to do it.

Link to comment
Share on other sites

vampke,

 

ensure your not trying to set database values that are automatically updated. I.E. time, date, auto_increment id's, etc.

 

Also - if you have access to run the update via phpMyAdmin, do so and see what errors you are getting.

Link to comment
Share on other sites

vampke,

 

ensure your not trying to set database values that are automatically updated. I.E. time, date, auto_increment id's, etc.

 

Also - if you have access to run the update via phpMyAdmin, do so and see what errors you are getting.

 

Thank you very much everybody that was trying to help me!

I am VERY sorry and ashamed to say that I have sold the problem myself....

I misnamed the id box in the html code....

:-[ :-[ :-[ :-[ :-[ :-[ :-[

 

Link to comment
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.