Jump to content

entering information into mysql database from php... SEE INSIDE...


Recommended Posts

ok heres the situation, i need to create a shop so that once an item is clicked... it then goes on and the user enters personal information which is then sent and added to the customer_info table i created in my mysql database...

 

ok so i have all that done except for the part where i get the user to enter information:

 

i have to files, see below

<?php
// Connect to the DB
$Sort = $_GET['Sort'];
$Submit = $_GET['submit'];
//echo "<hr><h3>Results sorted by $Sort</h3>";
//echo "\$submit is " . $submit;
//echo "\$Sort is " . $Sort;
if($Submit == "Submit Query"){
require "mysql_connect_lec.php";

switch($Sort)
{
	case "id":  $sort_var = "product_id"; break;
	case "name":  $sort_var = "product_name"; break;
	case "price": $sort_var = "product_price"; break;
}

// Create a query string
$sql_query = "SELECT * FROM product_info ORDER BY $sort_var";

// Run the query and save the results
$result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());

// Start creating a table to display the results [static part]
echo '<table border="1"><tr><th>Product ID</th><th>Team Name</th><th>Price</th></tr>';

// loop through the results - create as many table rows as there are records [dynamic part]
while ($row = mysql_fetch_array($result)) {
  // this sticks the results row by row into an array called $row.
  // rows are called via $array_name["COLUMN_NAME"]
	$out  = '<tr>';
	$out .= '<td>'.$row["product_id"].'</td>';
	$out .= '<td>'.$row["product_name"].'</td>';
	$out .= '<td><a href="mysql_lec_4.php?product_name='.$row["product_id"].'">'.$row["product_price"].'</a></td>';
	$out .= '</tr>';
	echo $out;
}

// Finish constructing the table [static part]
echo '</table>';

// Close the connection
@mysql_close($mysql_conn) or die("Error whilst closing connection: " . mysql_error());
} // End IF statement
?>

 

everything above seems to work fine i am just pasting so you can understand it more...

 

here is the bit i have a problem with:

<?php
require "mysql_connect_lec.php";
$Rank = $_GET['Rank'];
echo "the Rank I have got is ". $product_name;

if($submit == "Update"){
    $Rank = $CR['Rank'];
    echo "Now I have got Rank is " . $product_name;
$sql_query = "UPDATE product_info SET ";
$sql_query .= "Rank = '".$HTTP_POST_VARS['product_name'];
    $sql_query .= "', Gross = '".$HTTP_POST_VARS['product_id'];
    $sql_query .= "', Year = '".$HTTP_POST_VARS['product_price']."' WHERE Rank=".$product_name;
echo $sql_query;

$result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());

}
$sql_query = "SELECT * FROM product_info WHERE product_name=$product_name";
$result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());
$CR = mysql_fetch_assoc($result);
echo $CR['Name'];
@mysql_close($mysql_conn) or die("Error whilst closing connection: " . mysql_error());


?>
<H1>Record update form</H1>
<form name="form1" method="get" action="mysql_lec_4a.php">
  Rank   <input name="Rank" type="text" value="<?php echo $CR['Rank'] ?>">   <br>
  Gross sale   <input type="text" name="Gross" value="<?php echo $CR['Gross'] ?> "> <br>
  Name   <input type="text" name="Name" value=<?php echo $CR['Name'] ?>> <br>
  Year   <input type="text" name="Year" value="<?php echo $CR['Year'] ?>">
  <input name="MID" type="hidden" value="<?php echo $MID ?>">
  <input name="show" type="hidden" value="1">

  <input name="submit" type="submit" value = "Update">
<hr>
  <h2><a href="mysql_lec_3.php?Sort=Name&submit=Submit+Query">Box Office listing</a> </h2>
</form>

 

ok so i have tried to edit this myself with trial and error but no luck, it doesnt submit the users information into the customer_info table.

 

i have my customer_info table setup like this

 

mysql> select * from customer_info;
+-----------+--------------+----------------+-----------+-----------+----------+---------------------------+
| Name      | Address_1    | Address_2      | City      | Postcode  | Quantity | Comments                  |
+-----------+--------------+----------------+-----------+-----------+----------+---------------------------+
| name test | address test | address test 2 | city test | post test | 1        | This is working correctly |
+-----------+--------------+----------------+-----------+-----------+----------+---------------------------+

 

 

ok here is my the one i edited to try and get to send info to customer_info table but wont enter it... i understand that the mysql_lec_4a.php is the file where the user will be sent after for example... THANKS FOR ENTERING INFO...

<?php
require "mysql_connect_lec.php";
$Rank = $_GET['Rank'];
echo "the Rank I have got is ". $product_name;

if($submit == "Update"){
    $Rank = $CR['Rank'];
    echo "Now I have got Rank is " . $product_name;
$sql_query = "UPDATE customer_info SET ";
$sql_query .= "Name = '".$HTTP_POST_VARS['Name'];
    $sql_query .= "', Address_1 = '".$HTTP_POST_VARS['Address_1'];
  $sql_query .= "', Address_2 = '".$HTTP_POST_VARS['Address_2'];
  $sql_query .= "', City = '".$HTTP_POST_VARS['City'];
$sql_query .= "', Postcode = '".$HTTP_POST_VARS['Postcode'];
$sql_query .= "', quantity = '".$HTTP_POST_VARS['Quantity'];
    $sql_query .= "', price = '".$HTTP_POST_VARS['Comments']."' WHERE name=".$comments;
echo $sql_query;

$result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());

}
$sql_query = "SELECT * FROM product_info WHERE product_name=$product_name";
$result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());
$CR = mysql_fetch_assoc($result);
echo $CR['Name'];
@mysql_close($mysql_conn) or die("Error whilst closing connection: " . mysql_error());


?>
<H1>Record update form</H1>
<form name="form1" method="get" action="mysql_lec_4a.php">
  id   <input name="Rank" type="text" value="<?php echo $CR['id'] ?>">   <br>
  name   <input type="text" name="Gross" value="<?php echo $CR['name'] ?> "> <br>
  price   <input type="text" name="Year" value="<?php echo $CR['price'] ?>">
  <input name="MID" type="hidden" value="<?php echo $MID ?>">
  <input name="show" type="hidden" value="1">

  <input name="submit" type="submit" value = "Update">
<hr>
  <h2><a href="mysql_lec_3.php?Sort=Name&submit=Submit+Query">Box Office listing</a> </h2>
</form>

 

 

My new, and its still not entering any information into the table:

mysql> select * from customer_info;
+-----------+--------------+----------------+-----------+-----------+----------+---------------------------+
| Name      | Address_1    | Address_2      | City      | Postcode  | Quantity | Comments                  |
+-----------+--------------+----------------+-----------+-----------+----------+---------------------------+
| name test | address test | address test 2 | city test | post test | 1        | This is working correctly |
+-----------+--------------+----------------+-----------+-----------+----------+---------------------------+
1 row in set (0.00 sec)

 

 

<?php
require "mysql_connect_lec.php";
$product_name = $_GET['product_name'];
echo "The product you have ordered is ". $product_name;

if($_POST['Submit'] == "Update") { //note... its a capital S as well
    $product_name = $CR['product_name'];
    echo "The product you have ordered is " . $product_name;
$sql_query = "UPDATE customer_info SET ";
$sql_query .= "Name = '".$HTTP_POST_VARS['Name'];
    $sql_query .= "', Address_1 = '".$HTTP_POST_VARS['Address_1'];
$sql_query .= "', Address_2 = '".$HTTP_POST_VARS['Address_2'];
$sql_query .= "', City = '".$HTTP_POST_VARS['City'];
$sql_query .= "', Postcode = '".$HTTP_POST_VARS['Postcode'];
$sql_query .= "', Quantity = '".$HTTP_POST_VARS['Quantity'];
    $sql_query .= "', Comments = '".$HTTP_POST_VARS['Comments']."' WHERE name=".$product_name;
echo $sql_query;

$result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());

}
$sql_query = "SELECT * FROM product_info WHERE product_name=$product_name";
$result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());
$CR = mysql_fetch_assoc($result);
echo $CR['Name'];
@mysql_close($mysql_conn) or die("Error whilst closing connection: " . mysql_error());


?>
<H1>Record update form</H1>
<form name="form1" method="get" action="mysql_lec_4a.php">
  Name   <input name="Rank" type="text" value="<?php echo $CR['Name'] ?>">   <br>
  Address Line 1   <input type="text" name="Gross" value="<?php echo $CR['Address_1'] ?> "> <br>
  Address Line 2   <input type="text" name="Gross" value="<?php echo $CR['Address_2'] ?> "> <br>
  City   <input type="text" name="Gross" value="<?php echo $CR['City'] ?> "> <br>
  Postcode   <input type="text" name="Gross" value="<?php echo $CR['Postcode'] ?> "> <br>
  Quantity   <input type="text" name="Gross" value="<?php echo $CR['Quantity'] ?> "> <br>
  Comments   <input type="text" name="Year" value="<?php echo $CR['Comments'] ?>">
  <input name="MID" type="hidden" value="<?php echo $MID ?>">
  <input name="show" type="hidden" value="1">

  <input name="submit" type="submit" value = "Update">
<hr>
  <h2><a href="mysql_lec_3.php?Sort=Name&submit=Submit+Query">LINK USED FOR LATER...</a> </h2>
</form>

Either way still not working, has anyone at least done this before with a form and the info is submitted to the table without bein in on terminal.

 

like there must be something like:

 

INSERT INTO customer_info

values('$NAME, $ADDRESS_1 ...... ETC

 

so that when the submit button is entered it will enter it into the row like... ya know?

<?php
require "mysql_connect_lec.php";
$product_name = $_GET['product_name'];
echo "The product you have ordered is ". $product_name;

if($_POST['submit'] == "Update") {
    $product_name = $CR['product_name'];
    echo "The product you have ordered is " . $product_name;
$sql_query = "UPDATE customer_info SET ";
$sql_query .= "Name = '".$HTTP_POST_VARS['Name'];
    $sql_query .= "', Address_1 = '".$HTTP_POST_VARS['Address_1'];
$sql_query .= "', Address_2 = '".$HTTP_POST_VARS['Address_2'];
$sql_query .= "', City = '".$HTTP_POST_VARS['City'];
$sql_query .= "', Postcode = '".$HTTP_POST_VARS['Postcode'];
$sql_query .= "', Quantity = '".$HTTP_POST_VARS['Quantity'];
    $sql_query .= "', Comments = '".$HTTP_POST_VARS['Comments']."' WHERE name=".$product_name;
echo $sql_query;

$result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());

}
$sql_query = "SELECT * FROM product_info WHERE product_name=$product_name";
$result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());
$CR = mysql_fetch_assoc($result);
echo $CR['Name'];
@mysql_close($mysql_conn) or die("Error whilst closing connection: " . mysql_error());


?>
<H1>Record update form</H1>
<form name="form1" method="get" action="mysql_lec_4a.php">
  Name   <input name="Name" type="text" value="<?php echo $CR['Name'] ?>">   <br>
  Address Line 1   <input type="text" name="Address_1" value="<?php echo $CR['Address_1'] ?> "> <br>
  Address Line 2   <input type="text" name="Address_2" value="<?php echo $CR['Address_2'] ?> "> <br>
  City   <input type="text" name="City" value="<?php echo $CR['City'] ?> "> <br>
  Postcode   <input type="text" name="Postcode" value="<?php echo $CR['Postcode'] ?> "> <br>
  Quantity   <input type="text" name="Quantity" value="<?php echo $CR['Quantity'] ?> "> <br>
  Comments   <input type="text" name="Comments" value="<?php echo $CR['Comments'] ?>">
  <input name="MID" type="hidden" value="<?php echo $MID ?>">
  <input name="show" type="hidden" value="1">

  <input name="submit" type="submit" value = "Update">
<hr>
  <h2><a href="mysql_lec_3.php?Sort=Name&submit=Submit+Query">LINK USED FOR LATER...</a> </h2>
</form>

 

 

 

I see. UPDATE query is done when the record for product_name is already in your customer_info table and you are updating it. Otherwise, you should go with INSERT. Something like this:

 

$sql_query = "INSERT INTO customer_info (name, address_1, address_2, city, postcode, quantity, comments) VALUES('{$_POST['Name']}', '{$_POST['Address_1']}', '{$_POST['Address_2']}', '{$_POST['City']}', '{$_POST['Postcode']}', '{$_POST['Quantity']}', '{$_POST['Comments']}')";

 

ok but just to say i have 2 tables, product_info and customer_info. so product_name isn't needed to be submitted to a column in customer_info only product_id is sent into the table... erm i hope you understand what i am trying to say.

 

so what your saying is that like that snippet of code you have above needs to be put say here:

 

<?php

require "mysql_connect_lec.php";

$product_name = $_GET['product_name'];

echo "The product you have ordered is ". $product_name;

 

if($_POST['submit'] == "Update") {

    $product_name = $CR['product_name'];

    echo "The product you have ordered is " . $product_name;

$sql_query = "UPDATE customer_info SET ";

$sql_query .= "Name = '".$HTTP_POST_VARS['Name'];

    $sql_query .= "', Address_1 = '".$HTTP_POST_VARS['Address_1'];

$sql_query .= "', Address_2 = '".$HTTP_POST_VARS['Address_2'];

$sql_query .= "', City = '".$HTTP_POST_VARS['City'];

$sql_query .= "', Postcode = '".$HTTP_POST_VARS['Postcode'];

$sql_query .= "', Quantity = '".$HTTP_POST_VARS['Quantity'];

    $sql_query .= "', Comments = '".$HTTP_POST_VARS['Comments']."' WHERE name=".$product_name;

echo $sql_query;

 

$result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());

 

}

$sql_query = "SELECT * FROM product_info WHERE product_name=$product_name"; <<< $sql_query = "INSERT INTO customer_info (name, address_1, address_2, city, postcode, quantity, comments) VALUES('{$_POST['Name']}', '{$_POST['Address_1']}', '{$_POST['Address_2']}', '{$_POST['City']}', '{$_POST['Postcode']}', '{$_POST['Quantity']}', '{$_POST['Comments']}')";

 

$result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());

$CR = mysql_fetch_assoc($result);

echo $CR['Name'];

@mysql_close($mysql_conn) or die("Error whilst closing connection: " . mysql_error());

 

 

?>

<H1>Record update form</H1>

<form name="form1" method="get" action="mysql_lec_4a.php">

  Name   <input name="Name" type="text" value="<?php echo $CR['Name'] ?>">   <br>

  Address Line 1   <input type="text" name="Address_1" value="<?php echo $CR['Address_1'] ?> "> <br>

  Address Line 2   <input type="text" name="Address_2" value="<?php echo $CR['Address_2'] ?> "> <br>

  City   <input type="text" name="City" value="<?php echo $CR['City'] ?> "> <br>

  Postcode   <input type="text" name="Postcode" value="<?php echo $CR['Postcode'] ?> "> <br>

  Quantity   <input type="text" name="Quantity" value="<?php echo $CR['Quantity'] ?> "> <br>

  Comments   <input type="text" name="Comments" value="<?php echo $CR['Comments'] ?>">

  <input name="MID" type="hidden" value="<?php echo $MID ?>">

  <input name="show" type="hidden" value="1">

 

  <input name="submit" type="submit" value = "Update">

<hr>

  <h2><a href="mysql_lec_3.php?Sort=Name&submit=Submit+Query">LINK USED FOR LATER...</a> </h2>

</form>

 

 

Ok. When user hits Update button, we have product_name and we want to get product_id from product_info. Then insert this product_id along with customer information into customer_info, right? If so you can do this:

 

if($_POST['submit'] == "Update") {
  $sql_query = "SELECT * FROM product_info WHERE product_name=$product_name";
  $result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());
  $CR = mysql_fetch_assoc($result);
  product_id = $CR['product_id'];
  $sql_query = "INSERT INTO customer_info (product_id , name, address_1, address_2, city, postcode, quantity, comments) VALUES('{$_POST['Name']}', '{$product_id , $_POST['Address_1']}', '{$_POST['Address_2']}', '{$_POST['City']}', '{$_POST['Postcode']}', '{$_POST['Quantity']}', '{$_POST['Comments']}')";
  $result = @mysql_query($sql_query) or die("Invalid query: " . mysql_error());
}

 

I hope I got right this time.  :)

afraid not.

 

what is needed is.

 

forget about product_name and info etc that is not need anymore.

 

when the user selects a product, the only thing that is sent to the customer_info table is the product_id.

 

the titles under the customer_info table are

 

name address_1 address2 etc etc.... so the information that the user enters into the textbox goes into the columns in the customer_info table... you understand what i am saying??? i dont know how else to put it...?

 

just forget the product info i have done that.... just now onto that form and the information put it needs to be sent to database... yes?

 

EDIT: for that code you said... nothing works its a blank screen but from what you think i say its wrong either way... read up.. ^

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.