rob893 Posted April 15, 2011 Share Posted April 15, 2011 Ok, I am currently at Uni, with one of my units needing me to build a content management system. I have been learning PHP for about 2 months, so go easy on me. So I was developing happily, I was able to use the CMS to add a product, edit that product and delete it. I load it up today to carry on working on it, but find that adding a product no longer works. I investigate further and find that the HTML form on the "Add a product" section's processing page (createProduct.php) is not receiving the values from the form using the $_POST method, therefore the mysql query was containing empty values. I found this confusing as I had not changed any of the code. So i investigate further, creating a test php page, copying the $_POST method into here and echoing the variables. And.. it worked. Even more confused, I delved deeper and found that the culprit for the $_POST method not working was one of my includes. connection.php. Here is the connection.php include: <?php //1. Create database connection $connection = mysql_connect("localhost", "root"); //No password on the DB yet as it is only local. if (!$connection) { die("Database connection failed: " . mysql_error()); } //2. Select database to use $db_select = mysql_select_db("cms",$connection); if (!$db_select) { die("Database selection failed: " . mysql_error()); } ?> And here is createProduct.php; <?php require_once("../includes/connection.php");?> <?php require_once("../includes/functions.php");?> <?php ?> <?php //Populating the variables with values from the HTML form $Product_Name = mysql_prep($_POST['Product_Name']); $Price = mysql_prep($_POST['Price']); $Product_Information = mysql_prep($_POST['Product_Information']); $category_id = $_POST['category']; echo $Product_Name . "<br />"; echo $Price . "<br />"; echo $Product_Information . "<br />"; echo $category_id . "<br />"; ?> <?php //Creating and submitting the mysql query echo $category_id; $query = "INSERT INTO tblProducts( Product_Name, Price, Product_Information, category_id ) VALUES ( '{$Product_Name}', {$Price}, '{$Product_Information}', {$category_id} )"; echo $query; if (mysql_query($query, $connection)) { header("Location: createProduct.php"); exit; } //error handling else { echo "<p>Product creation failed.</p>"; echo "<p>" . mysql_error() . "</p>"; } ?> <?php mysql_close($connection); ?> So my question is.. why, when suddenly loading it up today after a few days absence would the database connection code be conflicting ONLY ON THIS PAGE. editing and deleting a product still work. I am truly stumped as this seems completley illogical and is probably something really obvious. Any help would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/233815-help-a-php-newbie/ Share on other sites More sharing options...
Muddy_Funster Posted April 15, 2011 Share Posted April 15, 2011 there's too many jumps here: load it = it's not working |--> copy content from createProduct.php to test.php, without changing page code or connection.php code = test.php working |-->decide connection.php is the problem, even though it works fine on two other different pages = Illogical how about filling in the blanks? Link to comment https://forums.phpfreaks.com/topic/233815-help-a-php-newbie/#findComment-1202041 Share on other sites More sharing options...
rob893 Posted April 15, 2011 Author Share Posted April 15, 2011 Sorry I will explain it in more detail. To test it, I copied the contents of createProduct.php into a new file test.php. I stripped it down to the the bare minimum with only its contents being: $Product_Name = mysql_prep($_POST['Product_Name']); $Price = mysql_prep($_POST['Price']); $Product_Information = mysql_prep($_POST['Product_Information']); $category_id = $_POST['category']; echo $Product_Name . "<br />"; echo $Price . "<br />"; echo $Product_Information . "<br />"; echo $category_id . "<br />"; This was just to see if there was an error in the way I was posting the contents of the form into this PHP page. The variables echoed with the correct values. I then started adding in the parts that I had stripped out ealier to see if I could locate the cause; creating the query, running the query and then the includes. The variables were correct on all occasions until I added in the connection include. I get this error: Notice: Undefined index: Product_Name in createProduct.php on line 27 Notice: Undefined index: Price in createProduct.php on line 28 Notice: Undefined index: Product_Information in createProduct.php on line 29 Notice: Undefined index: category in createProduct.php on line 30 INSERT INTO tblProducts( Product_Name, Price, Product_Information, category_id ) VALUES ( '', , '', ) Product creation failed. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' '', )' at line 4 This is what I was trying to explain earlier. Having the PHP code to create the DB connection causes my POST method to function incorrectly, so it runs a query with empty values thus throwing the error above. The thing is, this only happens on adding a product, every other page with the connection include is fine, including adding a producing category which is exactly the same code except a different database table. I hope this was easier to understand Link to comment https://forums.phpfreaks.com/topic/233815-help-a-php-newbie/#findComment-1202050 Share on other sites More sharing options...
Muddy_Funster Posted April 15, 2011 Share Posted April 15, 2011 Could you post the top 20 - 25 lines of each of the other two pages and your form statement please? Link to comment https://forums.phpfreaks.com/topic/233815-help-a-php-newbie/#findComment-1202053 Share on other sites More sharing options...
rob893 Posted April 15, 2011 Author Share Posted April 15, 2011 HTML Form for adding a product: <form action="createProduct.php" method="POST"> <p>Product name: <input type="text" name="Product_Name" value="" id="Product_Name" /> <br /> <p>Product category: <? //Selecting tblCategory data and looping it into an HTML forms drop down list $query = "SELECT * FROM tblCategory"; $result = mysql_query($query); echo "<select name='category'>\n"; while ($data = mysql_fetch_assoc($result)) { $name = $data['categoryName']; $id = $data['id']; echo "<option value=$id>$name</option>\n"; } echo "</select>\n"; ?> <p>Price: <input type="text" name="Price" value="" id="Price" /> </p> <p>Product Information:</p> <textarea cols="50" rows="4" name="Product_Information" id="Product_Information"></textarea> <br /> <br /> <input type="submit" value="Add Product"/> <input type=button onClick="parent.location='http://localhost/458691/CMS/product.php'" value='Cancel'> Adding a product Category: <?php require_once("../includes/connection.php");?> <?php require_once("../includes/functions.php");?> <?php $Category_Name = mysql_prep($_POST['Category_Name']); echo $Category_Name . "<br />"; $query = "INSERT INTO tblCategory( CategoryName ) VALUES ( '{$Category_Name}' )"; echo $query; if (mysql_query($query, $connection)) { header("Location: category4.php"); exit; } //error handling else { echo "<p>Product creation failed.</p>"; echo "<p>" . mysql_error() . "</p>"; } ?> <?php mysql_close($connection); ?> And editing a product: <?php error_reporting (E_ALL ^ E_NOTICE); //Hide undefined variable warning ?> <?php require_once("../includes/connection.php");?> <?php require_once("../includes/functions.php");?> <?php //Start a session to allow easy transfer of numerous variables over a number of PHP pages session_start(); $id = $_SESSION['id']; //creating the variables with updated values from the HTML form $Product_Name = mysql_prep($_POST['Product_Name']); $Price = mysql_prep($_POST['Price']); $Product_Information = mysql_prep($_POST['Product_Information']); //Creating the update query to update values in the product table $query = "UPDATE tblProducts SET Product_Name = '{$Product_Name}', Price = {$Price}, Product_Information = '{$Product_Information}' WHERE id = {$id}"; echo $query; if (mysql_query($query, $connection)) { header("Location: Product4.php"); exit; } //error handling else { echo "<p>Product update failed.</p>"; echo "<p>" . mysql_error() . "</p>"; } ?> <?php mysql_close($connection); ?> Link to comment https://forums.phpfreaks.com/topic/233815-help-a-php-newbie/#findComment-1202057 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.