Jump to content

Adding data to Database through html form, not working, URGHHH


BrainBoxMad

Recommended Posts

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]);
   } 
   
?>
Link to comment
Share on other sites

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]);
   } 
   
?>
Link to comment
Share on other sites

 I just notice these errors, there are probably more:

// You have this
<form action="sqltest.php" method"post">
// Should be this
<form action="sqltest.php" method="post">
// You have this
<input type="submit" value="ADD RECORD" />
// Should be this
<input type="submit" name="submit" value="ADD RECORD" />
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

what have you done to troubleshoot what your code is doing? have you checked if your code inside your conditional statement is even running? if the conditional statement is false, how about checking if the $_POST data being tested by that statement is what you expect?

 

programming is more than throwing down lines of code, trying it, and running to ask someone else when your code doesn't run. you have to test what your code and data are doing. you can echo progress messages to tell you if code is running. you can echo/print_r variables to see what they are.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.