Jump to content

Recommended Posts

i have be looking at google for a while and this was my last result.

 

i can't figure out what i am doing wrong

 

if someone can land a hand?

 

basically the information has been written to the db fine and dandy i see it there, i just can't get the update below to work so it populates the form and also shows it! it just gives me a blank page

<?
$id=$_GET['id'];
  mysql_connect("mysql.xxx.com", "usre", "pass") or die('Unable to connect to database' .mysql_error());
   mysql_select_db("contactsfind");
$query="SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_num_rows($result); 
mysql_close();

$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");

?>

<form action="updated.php">
<input type="hidden" name="ud_id" value="<? echo "$id"; ?>">
First Name: <input type="text" name="ud_first" value="<?=$first ?>"><br>
Last Name: <input type="text" name="ud_last" value="<? echo "$last"?>"><br>
Phone Number: <input type="text" name="ud_phone" value="<? echo "$phone"?>"><br>
Mobile Number: <input type="text" name="ud_mobile" value="<? echo "$mobile"?>"><br>
Fax Number: <input type="text" name="ud_fax" value="<? echo "$fax"?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<? echo "$email"?>"><br>
Web Address: <input type="text" name="ud_web" value="<? echo "$web"?>"><br>
<input type="Submit" value="Update">
</form>

<?
++$i;
} 
?>

Link to comment
https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/
Share on other sites

Your code is terrible. Never use short open tags, always check your queries succeed before using there result, make sure variables are set before executing code that relies on them, always sanitize data coming from outside & why loop when your only expecting one row?

 

<?php
if (isset($_GET['id'])) {
  mysql_connect("mysql.xxx.com", "usre", "pass") or die('Unable to connect to database' .mysql_error());
  mysql_select_db("contactsfind");
  $id = mysql_real_escape_string($_GET['id']);
  $query = "SELECT * FROM contacts WHERE id='$id' LIMIT 1";
  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      extract($row);
      // form goes here.
    } else {
      // no results found, handle error
    }
  } else {
    // query failed, handle error
  }
} else {
  // id not set, handle error
}
?>

Actually, I see you might actually want the loop after all. Just seems odd creating multiple forms but anyway....

 

<?php
if (isset($_GET['id'])) {
  mysql_connect("mysql.xxx.com", "usre", "pass") or die('Unable to connect to database' .mysql_error());
  mysql_select_db("contactsfind");
  $id = mysql_real_escape_string($_GET['id']);
  $query = "SELECT * FROM contacts WHERE id='$id'";
  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        extract($row);
        // form goes here.
      }
    } else {
      // no results found, handle error
    }
  } else {
    // query failed, handle error
  }
} else {
  // id not set, handle error
}
?>

to be honest this code i found on a tutorial , i copied some of it, fixed it up a bit, things i didn't like

 

and bam didn't work either way

 

 

so the following is not being used?

 

$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");

 

 

doesn't the above just loop through the results to be updated?

Nothing in your code updates anything.

 

The snippet in your last reply simply assigns values from the database columns, to there own respective variables.

 

This....

 

while ($row = mysql_fetch_assoc($result)) {
  extract($row);
}

 

Does exactly the same thing.

I have to admit i getting stupid this late at this..

 

i have done the following

<?
if (isset($_GET['id'])) {
  mysql_connect("****", "****", "****") or die('Unable to connect to database' .mysql_error());
   mysql_select_db("contactsfind");
       $id = mysql_real_escape_string($_GET['id']); 
    $query = "SELECT * FROM contacts WHERE id='$id'";
if ($result = mysql_query($query)) {  
  if (mysql_num_rows($result)) {     
   while ($row = mysql_fetch_assoc($result)) {   
        extract($row);

?>

<form action="updated.php">
<input type="hidden" name="ud_id" value="<? echo "$id"; ?>">
First Name: <input type="text" name="ud_first" value="<?=$first ?>"><br>
Last Name: <input type="text" name="ud_last" value="<? echo "$last"?>"><br>
Phone Number: <input type="text" name="ud_phone" value="<? echo "$phone"?>"><br>
Mobile Number: <input type="text" name="ud_mobile" value="<? echo "$mobile"?>"><br>
Fax Number: <input type="text" name="ud_fax" value="<? echo "$fax"?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<? echo "$email"?>"><br>
Web Address: <input type="text" name="ud_web" value="<? echo "$web"?>"><br>
<input type="Submit" value="Update">
</form>

<?
}    
} else {      // no results found, handle error   
echo ' no results found'
  } 
   } else {    // query failed, handle error 
   echo ' query failed'
    }
} else {  // id not set, handle error}
echo ' the id is not set'
}
?>

 

ugh i don't know what i am doing the form wont show at all.

 

I changed this and stil the form doesn't appear for the love of me!

 

I see it on dreamweaver fine, but when i upload it and load the update.php file.. the form is not there.

<?php
if (isset($_GET['id'])) {
  mysql_connect("xxxxxx", "tsxxxxx1", "xxxxxxx") or die('Unable to connect to database' .mysql_error());
   mysql_select_db("contactsfind");
       $id = mysql_real_escape_string($_GET['id']); 
    $query = "SELECT * FROM contacts WHERE id='$id'";
if ($result = mysql_query($query)) {  
  if (mysql_num_rows($result)) {     
   while ($row = mysql_fetch_assoc($result)) {   
        extract($row);

?>

<form action="updated.php">
<input type="hidden" name="ud_id" value="<? echo "$id"; ?>">
First Name: <input type="text" name="ud_first" value="<?=$first ?>"><br>
Last Name: <input type="text" name="ud_last" value="<? echo "$last"?>"><br>
Phone Number: <input type="text" name="ud_phone" value="<? echo "$phone"?>"><br>
Mobile Number: <input type="text" name="ud_mobile" value="<? echo "$mobile"?>"><br>
Fax Number: <input type="text" name="ud_fax" value="<? echo "$fax"?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<? echo "$email"?>"><br>
Web Address: <input type="text" name="ud_web" value="<? echo "$web"?>"><br>
<input type="Submit" value="Update">
</form>

<?php
}    
} else {      // no results found, handle error   

  } 
   } else {    // query failed, handle error 
   
    }
} else {  // id not set, handle error}
}
?>

You need to do this EVERYWHERE in your code -

Use the proper <?php tags.

 

Also, your hidden form field is not named id (assuming the form in the code you posted is what you expect to be submitting to the code in question.)

well, initially the code was different int he first post, therefore i changed it up according to the suggestion from thorpe ( thanks btw)

 

i have changed the form to the correct variable.

 

what i wanted this form to do is populate the entries from the database so i can edit it and submit the changes. though , i can't get the form to display

 

i have added the <?php tags but the echo said the id is not set

from what i understand, i can't just land on update.php and have the form populate right?

 

that would be useless right?

 

But if i click Update on a link and pass the variable for the ID - so get can work then i can populate the form right?

 

 

 

grk101, we only see the information you supply in your posts. Your code is retrieving the records that correspond to a specific id. Your code is getting that id from $_GET['id'], a GET parameter on the end of the URL. Since that does make sense as a method to display records to allow them to be edited (though if this was a real application, you would need to check if the current visitor is logged in and is permitted to access the records with that id) we must assume that you intended your code to work using this method or you would not have written it that way.

 

It is up to you to provide a way in your application of visiting the page with the code you are showing us so that the expected GET parameter is present on the end of the URL or you need to change the way your application works if you intended to supply the id using some other method.

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.