PHP / MYSQL
| From: | Chiew, Richard | Date: | Mon, 18 Feb 2002 20:41:23 +0000 |
| Subject: | PHP / MYSQL | ||
| Groups: | php.mirrors | ||
| Request: | Send a blank email to php-mirrors+get-8750@lists.php.net to get a copy of this message | ||
Hi, there, i am writing the following php code to insert, update, delete
data from the forms that i created and tried to deliver them to the
table that i created in mysql. But when i tried to view it in IE, it
displayed the following error message:
Warning: Supplied argument is not a valid MySQL result resource in
c:\program files\nusphere\apache\nsdocs\together.php on line 64
(the problem area is highlighted in red)
<html>
<body>
<?php
$host="localhost";
$user="root";
$password="mollier";
$databasename="mydb";
$connect=mysql_connect ($host, $user, $password);
if (!$connect)
echo "Sorry, didn't connect.";
$select=mysql_select_db ($databasename);
if (!$select)
echo "Sorry, didn't select database.";
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
$query="SELECT * FROM employees";
$result = mysql_query($query);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s %s</a> \n", $PHP_SELF,
$myrow["id"], $myrow["first"], $myrow["last"]);
printf("<a
href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>",
$PHP_SELF, $myrow["id"]);
}
}
?>
<P>
<a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>
<P>
<form method="post" action="<?php echo $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>
This had been bugging me for a long time, your kind support will be much
apprecited, thanks a million!
Richard