Forum Moderators: coopster

Message Too Old, No Replies

help! PHP/MYSQL

failing to write into database using a form

         

tapali

2:34 pm on Nov 16, 2003 (gmt 0)

10+ Year Member



I was reading the tutorials from webmonkey and everything was going so well until it was time write into the database. I jus can't seem to get my problem. the form shows up but I can neither add or edit records

this is my code

<html>
<body>
<?php
erroR_reporting(E_ALL);
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
if ($submit) {
// here if no ID then adding else we're editing
if ($id) {
$sql = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position' WHERE id='$id'";
} else {
$sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')";
}
// run SQL against the DB
$result = mysql_query($sql);
echo "Record updated/edited!<p>";
} elseif ($delete) {
// delete a record
$sql = "DELETE FROM employees WHERE id='$id'";
$result = mysql_query($sql);
echo "$sql Record deleted!<p>";
} else {
// this part happens if we don't press submit
if (!$id) {
// print the list if there is not editing
$result = mysql_query("SELECT * FROM employees",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s %s</a> \n", $_SERVER['PHP_SELF'], $myrow["id"], $myrow["first"], $myrow["last"]);
printf("<a href=\"%s?id=%s&delete=yes\"><img src=\"backg.jpg\"></a><br>", $_SERVER_['PHP_SELF'], $myrow["id"]);
}
}

?>

<P><a href="<?php echo $_SERVER['PHP_SELF']?>">ADD A RECORD</a>
<P><form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">

<?php

if ($id) {
// editing so select a record
$sql = "SELECT * FROM employees WHERE id='$id'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$id = $myrow["id"];
$first = $myrow["first"];
$last = $myrow["last"];
$address = $myrow["address"];
$position = $myrow["position"];
// print the id for editing
?>

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

<?php
}
?>

First name:<input type="Text" name="first" value="<?php echo $first?>"><br>
Last name:<input type="Text" name="last" value="<?php echo $last?>"><br>
Address:<input type="Text" name="address" value="<?php echo $address?>"><br>
Position:<input type="Text" name="position" value="<?php echo $position?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
}
?>
</body>
</html>

[edited by: jatar_k at 6:49 pm (utc) on Nov. 16, 2003]
[edit reason] compressed the code [/edit]

Robber

3:38 pm on Nov 16, 2003 (gmt 0)

10+ Year Member



Hi,

Welcome to tne boards.

It looks like you have the register globals option turned off which means variables are not automatically created for your form data.

So instead of using '$first' for example, you would need to use $_POST['first']

Where $_POST is a super vriable holding all of your form data that was submitted via the post method.

dreamcatcher

6:47 pm on Nov 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change your mysql lines to:


$sql = "UPDATE employees SET first='${_POST[first']}',last='${_POST['last']}',address='${_POST['address']}',position='${_POST['position']}' WHERE id='$id'";

and


$sql = "INSERT INTO employees (first,last,address,position) VALUES ('${_POST['first']}','${_POST['last']}','${_POST['address']}','${_POST['position']}')";

You should also use:

if ($_POST['submit'])

and

if ($_POST['id'])

One final thing, I don`t think your id check. ie: if ($id) will work because its checking against nothing. You need to query the database before the if($id) statement and fetch all the id`s, then do the check:

$query = "SELECT id FROM employees";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

if ($row[0] == $_POST['id])

blah blah

Hope that makes sense. And welcome to the forum. :)

tapali

9:37 pm on Nov 16, 2003 (gmt 0)

10+ Year Member



thank a million it worked