Jump to content

Why was database not updating?


kingnutter

Recommended Posts

I had a problem solved recently on the PHP boards.

 

The code below was not updating the database. Once I had added:

 

$wid=$_POST["wid"];

 

in the "// set up error list array" it worked fine.

 

Can anyone explain why this was necessary, as the string wid is a numeric unique identifier which I wouldn't have thought needed validating or updating.

 

Many thanks,

Gary

 

<html>

<head></head>

<body>

<!-- standard page header begins -->
<p> <p>

<table width="100%" cellspacing="0" cellpadding="5"> <tr>
    <td></td>
</tr>
<tr>
    <td bgcolor="Navy"<font color="White">
    <b><font size="4">
MEDIA THESAURUS</tr>
<td bgcolor="Green"<font color="White">
    <b><font size="4">
ADMIN EDIT
</b></font>
    </td>
</tr>
</table>
<!--Standard page header ends -->

<?php

// includes

include('conf.php');

include('functions.php');

// form not yet submitted

// display initial form with values pre-filled

if (!$_POST['submit'])

{

// check for record ID

if ((!isset($_GET['wid']) || trim($_GET['wid']) == ''))

{

die('Missing record ID!');

}

// open database connection

$connection = mysql_connect($host, $user, $pass) or die ('Unable to connect!');

// select database

mysql_select_db($db) or die ('Unable to select database!');

// generate and execute query

$wid = $_GET['wid'];

$query = "SELECT word, category, tags, def, pub, author, submitted, amended, flagged FROM words WHERE wid = $wid";

$result = mysql_query($query) or die ("Error in query: $query. " . 
mysql_error());

// if a result is returned

if (mysql_num_rows($result) > 0)

{

// turn it into an object

$row = mysql_fetch_object($result);

// print form with values pre-filled

?>

<table cellspacing="5" cellpadding="5">

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

<input type="hidden" name="wid" value="<?php echo $wid; ?>">

<tr>

<td valign="top"><b><font size="-1">Main Entry</font></b></td>

<td>

<input size="50" maxlength="50" type="text" name="word"

value="<?php echo $row->word; ?>">

</td>

</tr>

<tr>

<td valign="top"><b><font size="-1">Definition</font></b></td>

<td>

<input size="50" maxlength="500" type="text" name="def"

value="<?php echo $row->def; ?>">

</td>

</tr>

<tr>

<td valign="top"><font size="-1">Tags</font></td>

<td>

<input size="50" maxlength="200" type="text" name="tags"

value="<?php echo $row->tags; ?>">

</td>

</tr>

<tr>

<td colspan=2>

<input type="Submit" name="submit" value="Update">

</td>

</tr>

</form>

</table>

<?php

}

// no result returned

// print graceful error message

else

{

echo '<font size=-1>That definition could not be located in our database.</font>';

}

}

else

{

// set up error list array

$errorList = array();

$word = $_POST['word'];

$def = $_POST['def'];

$tags = $_POST['tags'];


// check for record ID

if ((!isset($_POST['wid']) || trim($_POST['wid']) == ''))

{

die ('Missing record ID!');

}

// validate text input fields

if (trim($_POST['word']) == '')

{

$errorList[] = 'Invalid entry: Word';

}

if (trim($_POST['def']) == '')

{

$errorList[] = "Invalid entry: Definition";

}

if (trim($_POST['tags']) == '')

{

$errorList[] = "Invalid entry: Tags";

}

// check for errors

// if none found...

if (sizeof($errorList) == 0)

{

// open database connection

$connection = mysql_connect($host, $user, $pass) or die ('Unable to connect!');

// select database

mysql_select_db($db) or die ('Unable to select database!');

// generate and execute query

$query = ("UPDATE words SET word = '$word', def = '$def', tags = '$tags', WHERE wid = '$wid'");

$result = mysql_query($query) or die ("Error in query: $query. " . 
mysql_error());

// print result

echo '<font size=-1>Update successful.';

echo '<a href=record.php>Go back to the main menu</a>.</font>';

// close database connection

mysql_close($connection);

}

else

{

// errors occurred

// print as list

echo '<font size=-1>The following errors were encountered:';

echo '<br>';

echo '<ul>';

for ($x=0; $x<sizeof($errorList); $x++)

{

echo "<li>$errorList[$x]";

}

echo '</ul></font>';

}

}

?>

<!-- standard page footer -->

</body>

</html>

Link to comment
Share on other sites

hi...again...

ur posting the selected value to be updated  from the form below, u can also update via ..

"UPDATE words SET word = '$word', def = '$def', tags = '$tags', WHERE word = '$word', "

but its not a correct way...since  $wid is the unique identifier for the selected values in the form..

its the unique id which shows database which row has to  be updated....

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

 

<input type="hidden" name="wid" value="<?php echo $wid; ?>">

 

<tr>

 

<td valign="top"><b><font size="-1">Main Entry</font></b></td>

 

<td>

 

<input size="50" maxlength="50" type="text" name="word"

 

value="<?php echo $row->word; ?>">

 

</td>

 

</tr>

 

<tr>

 

<td valign="top"><b><font size="-1">Definition</font></b></td>

 

<td>

 

<input size="50" maxlength="500" type="text" name="def"

 

value="<?php echo $row->def; ?>">

 

</td>

 

</tr>

 

<tr>

 

<td valign="top"><font size="-1">Tags</font></td>

 

<td>

 

<input size="50" maxlength="200" type="text" name="tags"

 

value="<?php echo $row->tags; ?>">

 

</td>

 

</tr>

 

<tr>

 

<td colspan=2>

 

<input type="Submit" name="submit" value="Update">

 

</td>

 

</tr>

 

</form>

 

Link to comment
Share on other sites

Thanks for that.

 

So is it prerequisite that all the fields in the form (even hidden ones) are set up in the error list array? $wid will not be changed so I am confused as to why it needs error-checking.

 

since some database table can have many identical fields  ...except the unique identifier id...

so its a necessary.... to update  tables using unique identifier...

 

Exactly what I was doing. UPDATE variable WHERE record = unique identifier. However, below you say it is incorrect:

 

ur posting the selected value to be updated  from the form below, u can also update via ..

"UPDATE words SET word = '$word', def = '$def', tags = '$tags', WHERE word = '$wid', "

but its not a correct way...since  $wid is the unique identifier for the selected values in the form..

its the unique id which shows database which row has to  be updated...

 

I was doing both. Please someone show me what the correct way is (even though this is working)

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.