![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
New Coder ![]() Join Date: Oct 2008
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
cant find my error
i need to add data to my database and when i added everything after isset the page stopped working...i get this error...
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/students/got5758/public_html/hmwk7.php on line 35 ive been looking for my error for hours...maybe someone here can find it..it wud be greatly appreciated..oh and before asked..yes i am connected to my db..just left it out of this code Code:
<html>
<head>
<title>
</title>
</head>
<body>
<h3>Add a new product to the Inventory</h3>
<form method="post" action="<?php $_SERVER['php_self'];?>">
Enter Category<br />
<input type="text" name="category" size="20"><p />
Enter Product Name<br />
<input type="text" name="pro_name" size="20"><p />
Enter Price<br />
<input type="text" name="price" size="20"><p />
Enter Amount On-Hand<br />
<input type="text" name="onhand" size="20"><p />
<input type="hidden" name="do_php" value="true">
<input type="submit" value="Add Product">
</form>
<?php
if( isset($_POST['do_php'] ) )
{
$in_query = "insert into Inventory values(";
$in_query .= "NULL,'" .$_POST['category']. "',";
$in_query .= "'" .$_POST['pro_name'] ."','" . $_POST['price'] ."'";
$in_query .= "," . $_POST['onhand']")";
$in_result = mysql_query( $in_query )
or die("Could not connect" . mysql_error() );
if( !$in_result )
echo "Sorry, an error occurred\n";
else
echo "New Inventory was successfully added to the database!";
}
?>
</body>
</html>
|
|
|
|
|
|
PM User | #2 |
|
God Emperor ![]() ![]() Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
![]() ![]() ![]() |
Fresh set of eyes:
PHP Code:
PHP Code:
<?php $_SERVER['php_self'];?>. The first is because you're missing an echo to actually print it out, but also because php_self doesn't exist - its PHP_SELF you're looking for; however, don't use PHP_SELF since its XSS exploitable, use $_SERVER['SCRIPT_NAME'] instead which will take the currently executing script. If you include this into another file, you could use basename(__FILE__) instead.
__________________
Code:
struct User *upFou;
userInit(upFou, "Fou-Lu", 1);
printf("%s has %s to %s\n", (*upFou).Name, !quitSmoking(upFou) ? "FAILED" : "SUCCEEDED", (*upFou).Smoker == 1 ? "FAIL" : "PASS");
// Fou-Lu has FAILED to FAIL? Lol
|
|
|
|
|
|
PM User | #4 |
|
New Coder ![]() Join Date: Oct 2008
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
now im wondering if i could get some more help...i have to check if the data being added already exists and if it does then dont add it..if it doesnt add it...i've been googling it and cant understand most of whats out there...im just learning sql
|
|
|
|
|
|
PM User | #5 | |
|
God Emperor ![]() ![]() Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
![]() ![]() ![]() |
Quote:
MySQL actually has a REPLACE command which will either insert or update without needing to determine if it exists first. But you need to be handling it based on you're primary key, and from the looks of this I'd suspect that is what the NULL value is representing? Without knowing that, I would have to assume that each entry should be unique.
__________________
Code:
struct User *upFou;
userInit(upFou, "Fou-Lu", 1);
printf("%s has %s to %s\n", (*upFou).Name, !quitSmoking(upFou) ? "FAILED" : "SUCCEEDED", (*upFou).Smoker == 1 ? "FAIL" : "PASS");
// Fou-Lu has FAILED to FAIL? Lol
|
|
|
|
|
|
|
PM User | #7 |
|
God Emperor ![]() ![]() Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
![]() ![]() ![]() |
http://dev.mysql.com/doc/refman/5.1/en/replace.html
Code:
REPLACE INTO table (col1, ...coln) VALUES (val1, ...valn) That said, there really isn't a way to determine if this exists. Unless one of the other fields is actually responsible for controlling what is unique, in which case that should probably be you're primary key and a replace would work. Note that a replace updates any values that have changed as opposed to actually rejecting an insertion.
__________________
Code:
struct User *upFou;
userInit(upFou, "Fou-Lu", 1);
printf("%s has %s to %s\n", (*upFou).Name, !quitSmoking(upFou) ? "FAILED" : "SUCCEEDED", (*upFou).Smoker == 1 ? "FAIL" : "PASS");
// Fou-Lu has FAILED to FAIL? Lol
|
|
|
|
|
|
PM User | #9 |
|
God Emperor ![]() ![]() Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
![]() ![]() ![]() |
Mkay, what you can do for that then without changing you're structure is to first query for the productName, and insert only if the count is 0.
PHP Code:
Also, normally I wouldn't give this much help for a homework assignment since I just noticed it was one. For that reason, I'll say you shouldn't be too worried about SQL injections by this point unless thats already been covered in you're course.
__________________
Code:
struct User *upFou;
userInit(upFou, "Fou-Lu", 1);
printf("%s has %s to %s\n", (*upFou).Name, !quitSmoking(upFou) ? "FAILED" : "SUCCEEDED", (*upFou).Smoker == 1 ? "FAIL" : "PASS");
// Fou-Lu has FAILED to FAIL? Lol
|
|
|
|
|
|
PM User | #10 |
|
New Coder ![]() Join Date: Oct 2008
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
its a really long story..yes it is a hmwk assignment..no the teacher hasnt gone over some of this yet...and he encourages us to find the answer whereever we can...and most of the sql we were suppose to learn in a different class..but that teacher decided he doesnt know sql and is not going to teach it to us...go figure...but i really do appreciate your help
|
|
|
|
|
|
PM User | #11 |
|
New Coder ![]() Join Date: Oct 2008
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
i think i may have done something wrong...i get this Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/students/got5758/public_html/hmwk7.php on line 32
Code:
<html>
<head>
<title>
</title>
</head>
<body>
<h3>Add a new product to the Inventory</h3>
<form method="post" action="<?php $_SERVER['php_self'];?>">
Enter Category<br />
<input type="text" name="category" size="20"><p />
Enter Product Name<br />
<input type="text" name="pro_name" size="20"><p />
Enter Price<br />
<input type="text" name="price" size="20"><p />
Enter Amount On-Hand<br />
<input type="text" name="onhand" size="20"><p />
<input type="hidden" name="do_php" value="true">
<input type="submit" value="Add Product">
</form>
<?php
if( isset($_POST['do_php'] ) )
{
$sQry*=*"select***from*Inventory*where*product_name='"*.*$_POST['pro_name']*.*"'";
if*($result*=*mysql_query($sQry))
{
**** if*(mysql_num_rows($result)*>*0)
****
******** die('Cannot*insert*a*new*product*with*name*'*.*$_POST['pro_name']);
****
**** else
**** {
*****
$in_query = "insert into Inventory values(";
$in_query .= "NULL,'" .$_POST['category']. "',";
$in_query .= "'" .$_POST['pro_name'] ."','" . $_POST['price'] ."'";
$in_query .= "," . $_POST['onhand'] .")";
$in_result = mysql_query( $in_query )
or die("Could not connect" . mysql_error() );
if( !$in_result )
echo "Sorry, an error occurred\n";
else
echo "New Inventory was successfully added to the database!";
**** }
}
}
?>
</body>
</html>
|
|
|
|
|
|
PM User | #13 |
|
God Emperor ![]() ![]() Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
![]() ![]() ![]() |
While it shouldn't be based on the *'s (that would trigger the error at line 28 from the looks of it), I'm not certain what the problem is. Mine is almost identical (I shifted some things so that I could actually use some data, you'll need to rename to match you're tables):
PHP Code:
Code:
<html> <head> <title> </title> </head> <body> <h3>Add a new product to the Inventory</h3> <form method="post" action=""> Enter Category<br /> <input type="text" name="category" size="20"><p /> Enter Product Name<br /> <input type="text" name="pro_name" size="20"><p /> Enter Price<br /> <input type="text" name="price" size="20"><p /> Enter Amount On-Hand<br /> <input type="text" name="onhand" size="20"><p /> <input type="hidden" name="do_php" value="true"> <input type="submit" value="Add Product"> </form> A product with the name test prod already exists!</body> </html>
__________________
Code:
struct User *upFou;
userInit(upFou, "Fou-Lu", 1);
printf("%s has %s to %s\n", (*upFou).Name, !quitSmoking(upFou) ? "FAILED" : "SUCCEEDED", (*upFou).Smoker == 1 ? "FAIL" : "PASS");
// Fou-Lu has FAILED to FAIL? Lol
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|