Jump to content

[SOLVED] how come I can't get UPDATED record to display back, unles I refresh page twice?


Recommended Posts

Hi,

 

I am trying to work out this SELECT/UPDATE script here below and I almost got it to work, but no matter what I do, can't get the script to display back the "Updated" record correctly; unless I like refresh my browser twice, then it displays it right...

 

Appreciate any feedback!

 

<CODE>

 

<?php

 

// connection info

$host = 'housername';

$password = 'password';

$database = 'database_name';

$con = mysql_pconnect($host,$user,$password) or die('No Connection Found');

 

mysql_select_db($database,$con) or die('No Database Found');

 

$submit = $_POST['submit'];

$id = $_GET['id'];

$id_update = $_POST['recordID'];

$title = $_POST['title'];

$body = $_POST['body'];

 

$result = mysql_query("SELECT * from blog WHERE blog_id = $id");

if(isset($id)) {

$row = mysql_fetch_array($result) or die('No record found');

}

 

if (isset($submit)){

mysql_query("UPDATE blog SET blog_title = '$title', blog_body = '$body' WHERE blog_id = '$id_update'");

}

?>

 

 

 

<form id="form1" name="form1" method="POST" action="">

  <p>post title:

    <input name="title" type="text" id="title" value="<?php echo $row['blog_title'];?>"/>

  </p>

  <p>post body:

    <input name="body" type="text" id="body" value="<?php echo $row['blog_body'];?>" />

  </p>

  <p>

    <input name="recordID" type="hidden" id="recordID" value="<?php echo $row['blog_id'];?>" />

  </p>

  <p>

    <input type="submit" name="submit" value="submit" />

  </p>

</form>

<?php mysql_close($con); ?>

 

</CODE>

Move

if (isset($submit)){
   mysql_query("UPDATE blog SET blog_title = '$title', blog_body = '$body' WHERE blog_id = '$id_update'");
   }

 

before

$result = mysql_query("SELECT * from blog WHERE blog_id = $id");
if(isset($id)) {
$row = mysql_fetch_array($result) or die('No record found');
} 

 

Change

if (isset($submit))

to

if (isset($_POST['submit']))

Thanks Wildteen!!!  right on the dot... worked right off

 

I guess I do need to really watch the sequence of executions; I messed with it so much, but the one thing I hadnt done was to place the UPDATE line before the $result!!!  makes sense...( first update, then echo back...) was so close!!

 

Thanks a lot!

 

hey, one more quick question: I noticed one can use the mysql_connect, or also the mysql_pconnect, which one is best? and is it correct that one doenst need to close the connection with the latter?

mysql_pconnect use a persistent connections, which means the connection to the mysql server is never closed until you use mysql_close.

 

mysql_connect does not use a persistent connection and the connection is terminated once the script has finished being parsed.

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.