PHP/MYSQL question please help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

powaytitan
Forum Newbie
Posts: 10
Joined: Sat Mar 06, 2010 10:43 pm

PHP/MYSQL question please help!

Post by powaytitan »

The goal of this project is to create 3 php scripts one to add, one to update and one to delete information from a mysql database. I have the add and the delete done but I am having trouble with the update. When you select the record to update the php script is suppose to populate the fields back into a form that can be submited to update the mysql database. The fields end up being blank on the update page and also the update script is not working. Any help would be greatly appriciated! The edit php script works fine so I dont believe that is where the error is.

edit.php

Code: Select all

 
<html>  
<head>  
 
<title>Untitled Document</title>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
</head>  
<body>  
<table>  
<tr>  
<td align="center">EDIT DATA</td>  
</tr>  
<tr>  
<td>  
<table border="1">  
<?  
include "connect.php";
 
$order = "SELECT * FROM product";  
$result = mysql_query($order);  
while ($row=mysql_fetch_array($result)){  
echo ("<tr><td>$row[productnumber]</td>");  
echo ("<td>$row[name]</td>");  
echo ("<td>$row[description]</td>");  
echo ("<td>$row[price]</td>");
echo ("<td>$row[weight]</td>");
echo ("<td>$row[qoh]</td>");
echo ("<td><a href=\"editform.php?
id=$row[productnumber]\">Edit</a></td></tr>");
}  
?>  
</table>  
</td>  
</tr>  
</table>  
</body>  
</html> 
 
 
editform.php

Code: Select all

 
 
<html>  
<head>  
<title>Form Edit Data</title>  
</head>  
<body>  
Edit Product Data
<?  
include "connect.php";
 
$order = "SELECT * FROM product  where productnumber ='$id'";  
$result = mysql_query($order);  
$row = mysql_fetch_array($result);  
?>
 
  
<form method="post" action="editdata.php">
  <p>
    <input type="hidden" name="id" value="<? echo "$row[productnumber]"?>">  
    Name: <input type="text" name="name"  size="20" value="<? echo "$row[name]"?>">
  </p>
  <p>Description:
  <input type="text" name="description" size="40"   value="<? echo ("$row[description]")?>">  
  </p>
  <p>
  Weight: <input type="text" name="weight" size="5"   value="<? echo "$row[weight]"?>">
  </p>
  <p>Price:
  <input type="text" name="price" size="10"   value="<? echo "$row[price]"?>">
  </p>
  <p>Quantity On Hand:
    <input type="text" name="description" size="4"   value="<? echo "$row[qoh]"?>">
  </p>
  <p>
    <input type="submit"   name="submit value" value="Edit">
  </p>
 
</form>   
</body>  
</html> 
  
 
editdata.php

Code: Select all

 
<?  
include "connect.php";
 
$order = "UPDATE  product SET name='$name',  description='$description', price='$price', weight ='$weight', qoh= $qoh'  WHERE   productnumber='$id'";  
mysql_query($order);  
header("location:edit.php");  
?> 
 
 
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: PHP/MYSQL question please help!

Post by jraede »

Where are you getting the $id variable in edit_form.php? It doesn't look like you are setting it anywhere...if edit.php works, you can get $id by using

Code: Select all

$id = $_GET['id'];
.

This is probably why nothing is showing up, $id is getting interpreted as 0 because you aren't setting it.
powaytitan
Forum Newbie
Posts: 10
Joined: Sat Mar 06, 2010 10:43 pm

Re: PHP/MYSQL question please help!

Post by powaytitan »

I added the line of code within the php portion of the editform and still nothing is showing up. And just to clairfiy the form is showing up but nothing is inside of the input fields......
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: PHP/MYSQL question please help!

Post by jraede »

Where did you add it? Can you post what editform.php looks like now?

Also, try replacing $result = mysql_query($order) with $result = mysql_query($order) or die(mysql_error()); . Let me know if you see a MySQL error.
powaytitan
Forum Newbie
Posts: 10
Joined: Sat Mar 06, 2010 10:43 pm

Re: PHP/MYSQL question please help!

Post by powaytitan »

Code: Select all

 
 
<html>  
<head>  
<title>Form Edit Data</title>  
</head>  
<body>  
Edit Product Data
<?  
include "connect.php";
 
$order = "SELECT * FROM product  where productnumber ='$id'";  
$result = mysql_query($order);  
$row = mysql_fetch_array($result);  
[color=#800000]$id = $_GET['id'];[/color]
?>
 
  
<form method="post" action="editdata.php">
  <p>
    <input type="hidden" name="id" value="<? echo "$row[productnumber]"?>">  
    Name: <input type="text" name="name"  size="20" value="<? echo "$row[name]"?>">
  </p>
  <p>Description:
  <input type="text" name="description" size="40"   value="<? echo ("$row[description]")?>">  
  </p>
  <p>
  Weight: <input type="text" name="weight" size="5"   value="<? echo "$row[weight]"?>">
  </p>
  <p>Price:
  <input type="text" name="price" size="10"   value="<? echo "$row[price]"?>">
  </p>
  <p>Quantity On Hand:
    <input type="text" name="description" size="4"   value="<? echo "$row[qoh]"?>">
  </p>
  <p>
    <input type="submit"   name="submit value" value="Edit">
  </p>
 
</form>   
</body>  
</html> 
  
 
 
 
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: PHP/MYSQL question please help!

Post by jraede »

You have define $id before you use it in the query. Take the line I gave you and put it above where you have $order = "SELECT .....
powaytitan
Forum Newbie
Posts: 10
Joined: Sat Mar 06, 2010 10:43 pm

Re: PHP/MYSQL question please help!

Post by powaytitan »

God you are a real lifesaver I appriciate it very much. This is the error im getting now which I was not before....

Warning: Cannot modify header information - headers already sent by (output started at /home/pm0663/public_html/com288/Project6/connect.php:5) in /home/pm0663/public_html/com288/Project6/editdata.php on line 6
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: PHP/MYSQL question please help!

Post by jraede »

Seems like connect.php has a <head> tag, or is echoing something. Since it's an include file, it shouldn't have any html or echo statements whatsoever. Look through it and see if you can fix it. If not, post it here and someone can help.
powaytitan
Forum Newbie
Posts: 10
Joined: Sat Mar 06, 2010 10:43 pm

Re: PHP/MYSQL question please help!

Post by powaytitan »

connect.php only consists of two lines of code which connects to the database nothing else.
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: PHP/MYSQL question please help!

Post by jraede »

Well, your error says that line 5 of connect.php is outputting something to the browser. Can you just post the script?
powaytitan
Forum Newbie
Posts: 10
Joined: Sat Mar 06, 2010 10:43 pm

Re: PHP/MYSQL question please help!

Post by powaytitan »

of course

Code: Select all

 
<?  
mysql_connect("localhost","pm0663","say76poj");  
mysql_select_db("pm0663");  
?> 
 
powaytitan
Forum Newbie
Posts: 10
Joined: Sat Mar 06, 2010 10:43 pm

Re: PHP/MYSQL question please help!

Post by powaytitan »

This has been a frusterating evening for me but I have learned alot. If someone could please help I have written my update statement, it acts as the update has occured but in fact there has been no change to the database.

Code: Select all

 
 
<?  
$dbhost = 'localhost';
$dbuser = 'pm0663';
$dbpass = 'say76poj';
 
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
 
$dbname = 'pm0663';
 
mysql_select_db($dbname);
<?
    $id = $_GET['id'];
    $name = $_GET['name'];
    $description = $_GET["description"];
    $price = $_GET["price"];
    $weight = $_GET["weight"];
    $qoh = $_GET["qoh"];
    $sql = "UPDATE  product SET name='$name',  
        description='$description', 
        price='$price', 
        weight ='$weight', 
        qoh= $qoh'  
        WHERE   productnumber='$id'" or die(mysql_error());  
    $result = mysql_query($sql);  
    header("location:edit.php");  
 
?> 
 
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: PHP/MYSQL question please help!

Post by mikosiko »

something is missing here

Code: Select all

       weight ='$weight', 
       [color=#FF4000] qoh= $qoh'  [/color]
 
also you are not being consistent in the usage of ' and " like here:

Code: Select all

 
    $id = $_GET['id'];
    $name = $_GET['name'];
    $description = $_GET["description"];
    $price = $_GET["price"];
    $weight = $_GET["weight"];
 
powaytitan
Forum Newbie
Posts: 10
Joined: Sat Mar 06, 2010 10:43 pm

Re: PHP/MYSQL question please help!

Post by powaytitan »

I had alread fixed the missing ' and changed all of them to ' ' instead of a mixture with double quotes. This has not fixedI my problem. Also I added

$result = mysql_query($order) or die (mysql_error()) and no sql error is coming up.

Obviously I am new to php programming. Thanks again.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: PHP/MYSQL question please help!

Post by mikosiko »

powaytitan wrote:I had alread fixed the missing ' and changed all of them to ' ' instead of a mixture with double quotes. This has not fixedI my problem. Also I added

$result = mysql_query($order) or die (mysql_error()) and no sql error is coming up.

Obviously I am new to php programming. Thanks again.

Code: Select all

 
[color=#FF4000]<?   <-------   WHERE ARE YOU CLOSING THIS?[/color]
$dbhost = 'localhost';
$dbuser = 'pm0663';
$dbpass = 'say76poj';
 
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
 
$dbname = 'pm0663';
 
mysql_select_db($dbname);
<?
....
 
Post Reply