Jump to content

BrainBoxMad

Members
  • Posts

    12
  • Joined

  • Last visited

BrainBoxMad's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Made those changes Strider64, however, still, add functionality isn't working, i just can't get my head around it. I seperated the code for the add function, used it elswhere and it works, however using it wth the delete function as seen above just doesent work.
  2. I made some adjusment to this please ignore the above, However i am still not having any luck with the add functionality, please can anyone share some knowledge on this matter? <?php // Connect to server and select database require_once "config.php"; $dbhandle = mysql_connect($hostname, $username, $password); if (!$dbhandle) die("unable to connect to mysql: " . mysql_error()); mysql_select_db("store") or die ("unable to connect to databse: " . mysql_error()); // Get values from form if (isset($_POST['delete']) && isset($_POST['url'])) { // Delete data from mysql $url = get_post('url'); $query = "DELETE FROM laptop_products WHERE url = '$url'"; // if not successfully deleted data from database, displays message "delete failed". if (!mysql_query($query, $dbhandle)) echo "delete failed: $query<br />" . mysql_error() . "<br /><br />"; } if (isset($_POST['product_id']) && isset($_POST['product_name']) && isset($_POST['weight']) && isset($_POST['stock']) && isset($_POST['price'])&& isset($_POST['description'])&& isset($_POST['image'])&& isset($_POST['url']) && isset($_POST['ADD RECORD'])) //cODE SHOULD OLY RUN IF ADD RECORD BUTTON HAS BEEN PRESSED { $product_id = get_post('product_id'); $product_name = get_post('product_name'); $weight = get_post('weight'); $stock = get_post('stock'); $price = get_post('price'); $description = get_post('description'); $image = get_post('image'); $url = get_post('url'); $query = "INSERT INTO laptop_products ('product_id', 'product_name', 'weight', 'stock', 'price', 'description', 'image', 'url') VALUES ('$product_id','$product_name','$weight','$stock','$price','$description','$image','$url')"; $result=mysql_query($query); if (!mysql_query($query, $dbhandle)) echo "INSERT failes: $query<br />" . mysql_error() . "<br /><br />"; } echo <<<_END <form action="sqltest.php" method"post"><pre> product_id: <input type="text" name="product_id" /> product_name: <input type="text" name="product_name" /> weight: <input type="text" name="weight" /> stock: <input type="text" name="stock" /> price: <input type="text" name="price" /> description: <input type="text" name="description" /> image: <input type="text" name="image" /> url: <input type="text" name="url" /> <input type="submit" value="ADD RECORD" /> </pre></form> _END; // Selects all colums rows from table and displays results $query = "SELECT * FROM laptop_products"; $result = mysql_query($query, $dbhandle); //if not successful in selected data, display "database access failed" if (!$result) die ("Database access failed: " . mysql_error()); $rows = mysql_num_rows($result); //displays all records of tables, rather then just first one, which holds "product_id" 2 for ($j = '0' ; $j < $rows ; ++$j) { //returns the rows from table "laptop_products" $row = mysql_fetch_row($result); // data that needs to be displayed through above, function echo <<<_END <pre> product_id $row[0] product_name $row[1] weight $row[2] stock $row[3] price $row[4] description $row[5] image $row[6] url $row[7] </pre> <form action="sqltest.php" method="post"> <input type="hidden" name="delete" value="yes" /> <input type="hidden" name="url" value="$row[7]" /> <input type="submit" value="DELETE RECORD" /></form> _END; } mysql_close($dbhandle); function get_post($var) { return mysql_real_escape_string($_POST[$var]); } ?>
  3. Jon, yeah, that worked, thank you,really appreciated, thanks for the time. However, now after resolving this problem, the main aim of this code which is to select a value from the drop down list and in return display the table related to that value from the database is not happening. Life story to PHP, never ending problems ; )
  4. no seems not to work tried that before too, however, receive the following: Notice: Undefined variable: q in E:\website\htdocs\restricted\test\getproduct.php on line 24 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in E:\website\htdocs\restricted\test\getproduct.php on line 37
  5. Hi PHP explorers, well as i am sailing across picking up the knowledge required for PHP i have run in to a problem my pair of eyes can't spot. So i am here to ask to lend yours, I created a form that offers functionality to delete and add tables in to my database, however the ADD functionality does not work, and no errors appear. Below is all my code, however the section for ADD functionality is in the colour green : ) Thanks <?php require_once "config.php"; $dbhandle =mysql_connect($hostname, $username, $password); if (!$dbhandle) die("unable to connect to mysql: " . mysql_error()); mysql_select_db("store") or die ("unable to connect to databse: " . mysql_error()); if (isset($_POST['delete']) && isset($_POST['price'])) { $price = get_post('price'); $query = "DELETE FROM laptop_products WHERE price = '$price'"; if (!mysql_query($query, $dbhandle)) echo "delete failed: $query<br />" . mysql_error() . "<br /><br />"; } if (isset($_POST["product_id"]) && isset($_POST["product_name"]) && isset($_POST["weight"]) && isset($_POST["stock"]) && isset($_POST["price"])&& isset($_POST["desciprion"])&& isset($_POST["image"])&& isset($_POST["url"])) { $product_id = get_post("product_id"); $product_name = get_post("product_name"); $weight = get_post("weight"); $stock = get_post("stock"); $price = get_post("price"); $description = get_post("description"); $image = get_post("image"); $url = get_post("url"); $query = "INSERT INTO laptop_products VALUES" . "('$product_id', '$product_name', '$weight', '$stock', '$price')"; if (!mysql_query($query, $dbhandle)) echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />"; } echo <<<_END <form action="sqltest.php" method"post"><pre> product_id <input type="text" name="product_id" /> product_name <input type="text" name="product_name" /> weight <input type="text" name="weight" /> stock <input type="text" name="stock" /> price <input type="text" name="price" /> description <input type="text" name="description" /> image <input type="text" name="image" /> url <input type="text" name="url" /> <input type="submit" value="ADD RECORD" /> </pre></form> _END; $query = "SELECT * FROM laptop_products"; $result = mysql_query($query); if (!$result) die ("Database access failed: " . mysql_error()); $rows = mysql_num_rows($result); for ($j = '0' ; $j < $rows ; ++$j) { $row = mysql_fetch_row($result); echo <<<_END <pre> product_id $row[0] product_name $row[1] weight $row[2] stock $row[3] price $row[4] description $row[5] image $row[6] url $row[7] </pre> <form action="sqltest.php" method="post"> <input type="hidden" name="delete" value="yes" /> <input type="hidden" name="url" value="$row[7]" /> <input type="submit" value="DELETE RECORD" /></form> _END; } mysql_close($dbhandle); function get_post($var) { return mysql_real_escape_string($_POST[$var]); } ?>
  6. Yes that is true sorry, well i am receiving the same error message Notice: Undefined variable: q in E:\website\htdocs\restricted\test\getproduct.php on line 23 after using the advice that was offered above. The error is refering to line 23 which is: $sql="SELECT * FROM laptop_products WHERE product_id = '".$q."'"; from the above code in the php page any hints?
  7. Hi everyone i keep getting the error message Parse error: syntax error, unexpected end of file in E:\website\htdocs\restricted\test\sqltest.php on line 87 I am trying to display a form that offers me the functionality to add and delete rows from my database table. all help will be very appreciated : ) <?php require_once "config.php"; $dbhandle =mysql_connect($hostname, $username, $password); if (!$dbhandle) die("unable to connect to mysql: " . mysql_error()); mysql_select_db("store") or die ("unable to connect to databse: " . mysql_error()); if (isset($_POST['delete']) && isset($_POST['price'])) { $price = get_post('price'); $query = "DELETE FROM laptop_products WHERE price = '$price'"; if (!mysql_query($query, $dbhandle)) echo "delete failed: $query<br />" . mysql_error() . "<br /><br />"; } if (isset($_POST["product_id"]) && isset($_POST["product_name"]) && isset($_POST["weight"]) && isset($_POST["stock"]) && isset($_POST["price"])) { $product_id = get_post("product_id"); $product_name = get_post("product_name"); $weight = get_post("weight"); $stock = get_post("stock"); $price = get_post("price"); $query = "INSERT INTO laptop_products VALUES" . "('$product_id', '$product_name', '$weight', '$stock', '$price')"; if (!mysql_query($query, $dbhandle)) echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />"; } echo <<<_END <form action="sqltest.php" method"post"><pre> product_id <input type="text" name="product_id" /> product_name <input type="text" name="product_name" /> weight <input type="text" name="weight" /> stock <input type="text" name="stock" /> price <input type="text" name="price" /> <input type="submit" name="ADD RECORD" /> </pre></form> _END; $query = "SELECT * FROM laptop_products"; $result = mysql_num_rows($query); if (!$result) die ("Database access failed: " . mysql_error()); $rows = mysql_num_rows($result); for ($j = 0 ; $j < $rows ; ++$j) { $row = mysql_fetch_row($result); echo <<<_END <pre> product_id $row[0] product_name $row[1] weight $row [2] stock $row [3] price $row [4] </pre> <form action="sqltest.php" method="post"> <input type="hidden" name="delete" value="yes" / <input type="hidden" name="price" value="$row[4]" /> <input type="submit" value="DELETE RECORD" /></form> _END } mysql_close($dbhandle); function get_post($var) { return mysql_real_escape_string($_POST[$var]); } ?>
  8. Hey, i added the extra line of code but i am still for some reason still receiving the same error message :S I am sorry, but is there anything else another fresh pair of eyes my see what i am doing wrong?
  9. Hi people, i am basically trying to have the functionality of a dropdown menu that when chosen a specific value from it, the resulting display will be that values data from my databse table, this information gets displayed on a html format table. I am keep getting the error: Notice: Undefined variable: q in E:\website\htdocs\restricted\test\getproduct.php on line 17 Please have in mind i am a completely novice with PHP, and doing this just to get better, please if someone can help me get my head around this, it will be much appreciated. Thank You. HTML CODE is: <html> <head> <script> function showproduct(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","getproduct.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <form> <select name="laptop_products" onchange="showproduct(this.value)"> <option value="">Select a person:</option> <option value="1">SAMSUNG</option> <option value="2">HP</option> <option value="3">ASUS</option> <option value="4">LENOVO</option> </select> </form> <br> <div id="txtHint"><b>Person info will be listed here.</b></div> </body> </html> The PHP CODE is: <?php if (isset($_GET['q'])) { echo $_GET['q']; } $dbhandle = mysql_connect('localhost','root','','store'); if (!$dbhandle) { die("Could not connect: " . mysql_error()); } mysql_select_db("store") or die ("Could not connect: " . mysql_error()); $sql="SELECT * FROM laptop_products WHERE product_id = '".$q."'"; $result = mysql_query($sql); echo "<table border='1'> <tr> <th>product_id</th> <th>product_name</th> <th>weight</th> <th>price</th> <th>description</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['product_id'] . "</td>"; echo "<td>" . $row['product_name'] . "</td>"; echo "<td>" . $row['weight'] . "</td>"; echo "<td>" . $row['price'] . "</td>"; echo "<td>" . $row['description'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($dbhandle); ?>
  10. To Mac_gyver, Thanks for the above help, that got me sorted ; )
  11. Yes, thank you L2c, however, i am now getting the data displayed, but not correctly, as you can see below, any ideas why this may be happening? Connected to MySQL ID: 1 Name: ASUS 15" Ultrabook Product Type: Laptop Price: 400 Description: Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Test Gibrish Stock: 15 Weight: 8 ID Name Product Type Price Description Stock Weight
  12. Hi everyone, I am quite new to the world of PHP, and really need some help, as i am getting the following error message over and over again, and cant find a solution: Parse error: syntax error, unexpected 'while' (T_WHILE), expecting ',' or ';' in E:\website\htdocs\test\test.php on line 27 Can anyone please give me some of their time and check through my code to maybe see if they can spot the problem area? Thanks. My Code is the following: <?php include 'config.php'; //Choose the database $selected = mysql_select_db("store",$dbhandle) or die("Could not select store"); //execute the SQL query and return results $result = mysql_query("SELECT product_id, product_name, product_type, price, description, stock, weight FROM products"); echo "<table border=\"1\" cellpadding=\"3\">\n"; echo "<thead>\n"; echo "<tr>\n"; echo "<th>ID</th>"; echo "<th>Name</th>"; echo "<th>Product Type</th>"; echo "<th>Price</th>"; echo "<th>Description</th>"; echo "<th>Stock</th>"; echo "<th>Weight</th>"; echo "</thead>\n"; echo "<tbody>\n" //retrieve that data from the database, while loop due to having more than one result while($row = mysql_fetch_array($result)) { echo "ID: ".$row{"product_id"}. "<br>"; echo "Name: ".$row{"product_name"}. "<br>"; echo "Product Type: ".$row{"product_type"}. "<br>"; echo "Price: ".$row{"price"}. "<br>"; echo "Description: ".$row{"description"}. "<br>"; echo "Stock: ".$row{"stock"}. "<br>"; echo "Weight: ".$row{"weight"}. "<br>"; "<br>"; //display the results } ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.