Jump to content

insert into mysql database


Recommended Posts

I'm trying to insert data from another query into a database using the following code:

 

<?php

// Start a session
session_start();

// Sends the user to the login-page if not logged in
if(!session_is_registered('customerEmail')) :
header('Location: index.php?msg=requires_login');
endif;

// Include database information and connectivity
include 'db.php';
connect();
// We store all our functions in one file
include 'functions.php';

if (isset($_POST['submit'])) { 

$insert = "INSERT INTO orderdetails (productCode, currentPrice)
VALUES ('productCode, sellPrice')";
$add_member = mysql_query($insert);

?>

<html>

<head>

<title> www.sports-r-us.co.uk </title>

<link rel="stylesheet" type="text/css" href="registerpage.css"/>

</head>


<body>

<div id="site" align="center">

<div id="header">
<div class="text">
<a href="sitemap.html">SiteMap</a>
<a href="contactus.html">ContactUs</a>
<a href="help.html">Help</a>
</div>
</div>

<div id="navbar">

	<div class="text">
	<a href="index.html">Home</a> 
	<a href="login.html">ProductCatalogue</a> 
	<a href="login.html">Search</a> 
	<a href="login.html">BrowseByCategory</a> 
	<a href="login.html">Basket</a> 
	<a href="login.html">Login/Register</a>  
	</div> 
</div>

<div id="block">

	<h1>Registered</h1>
	<p>Thank you, your order has been placed.</a>.</p>
	<a href="product-catalogue.php">Product Catalogue</a>

</div>

<div id="footer">
</div>
</div>	

<?php 
} 
else 
{ 
?>


<html>

<head>

<title> www.sports-r-us.co.uk </title>

<link rel="stylesheet" type="text/css" href="sportsrus.css"/>

</head>


<body>

<div id="site" align="center">

<div id="header">
<div class="text">
<a href="sitemap.php">SiteMap</a>
<a href="contactus.php">ContactUs</a>
<a href="help.php">Help</a>
</div>


</div>

<div id="navbar">
	<div class="text">
	<a href="user_Home.php">Home</a> 
	<a href="product-catalogue.php">ProductCatalogue</a> 
	<a href="search.php">Search</a> 
	<a href="browse.php">BrowseByCategory</a> 
	<a href="basket.php">Basket</a> 
	<a href="logout.php">Logout</a> 
	</div> 
</div>

<?php	

$productCode = $_GET["ProductID"]; //reads the url and takes the productID= number
$customerEmail = $_GET["Customer"];	

$query="SELECT * FROM products WHERE productCode = '$productCode'"; //Find match in database where the productCode is the same as the URL. Should only be 1.
$result=mysql_query($query);

$productName=mysql_result($result,'0',"productName"); //Returns the productName of result 0.

$productCode=mysql_result($result,'0',"productCode");
$productName=mysql_result($result,'0',"productName");
$productLine=mysql_result($result,'0',"productLine");
//$productScale=mysql_result($result,'0',"productScale");
//$productVendor=mysql_result($result,'0',"productVendor");
$productDescription=mysql_result($result,'0',"productDescription");
//$quantityInStock=mysql_result($result,'0',"quantityInStock");
$image=mysql_result($result,'0',"image");
//$buyPrice=mysql_result($result,'0',"buyPrice");
$sellPrice=mysql_result($result,'0',"sellPrice");


$query2="SELECT * FROM customers WHERE customerEmail = '$customerEmail'"; //Find match in database where the productCode is the same as the URL. Should only be 1.
$result2=mysql_query($query2);

$cardNumber=mysql_result($result2,'0',"cardNumber"); //Returns the productName of result 0.
$cardType=mysql_result($result2,'0',"cardType");
$issueNumber=mysql_result($result2,'0',"issueNumber");
$startDate=mysql_result($result2,'0',"startDate");
$expiryDate=mysql_result($result2,'0',"expiryDate");
$nameOnCard=mysql_result($result2,'0',"nameOnCard");
$securityNumber=mysql_result($result2,'0',"securityNumber");


echo "<font color=\"white\">You want to buy: $productName</br>which costs £$sellPrice</font>";
echo "<font color=\"white\"></br></br>Please check that the following details are correct. If not, you must of registered incorrectly. If they are, please click 'buy' to complete your order</font>";
echo "<font color=\"white\"></br></br></br>Card number: $cardNumber</br>Card type: $cardType</font>";
echo "<font color=\"white\"></br>Issue Number: $issueNumber </br>startDate (--/--): $startDate</font>";
echo "<font color=\"white\"></br>Expiry Date (--/--): $expiryDate </br>Name on card: $nameOnCard</font>";
echo "<font color=\"white\"></br>Security Number: $securityNumber</font>";


?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">



<tr><th colspan=2><input type="submit" name="submit" value="Buy"></th></tr> </table>
</form>



<div id="footer">
</div>
</div>	

</body>

</html>

<?php
}
?> 

 

On lines 19-21, I want to insert the data from lines 125 & 134. But I get the errors messages:

 

 

Notice: Undefined variable: productCode in C:\wamp\www\checkout.php on line 20

 

Notice: Undefined variable: sellPrice in C:\wamp\www\checkout.php on line 20

 

Can someone please explain what I'm doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/157633-insert-into-mysql-database/
Share on other sites

Those variables are not defined yet. If you echo those variables out, you will find they are probably empty at that point, which means that you cannot insert the data that you want.

 

You will have to determine how those vars are set, and then repeat that process where you want to perform that query.

 

Nate

Those variables are not defined yet. If you echo those variables out, you will find they are probably empty at that point, which means that you cannot insert the data that you want.

 

You will have to determine how those vars are set, and then repeat that process where you want to perform that query.

 

Nate

 

I don't really understand what I have to do, I find it hard understanding any sort of technical talk lol.

Anyway, if i type in echo $sellPrice etc down the bottom somewhere, it echoes fine...but it just wont let me insert it in the database

Those vars are not defined yet in that stage in the code.

 

Lines 117 and 134 are as follows.

   $productCode=mysql_result($result,'0',"productCode");
   $sellPrice=mysql_result($result,'0',"sellPrice");

 

This is where those variables are defined, so if you echo them at the bottom, (below these lines) then they are set, but at line 19 those vars are not set yet.

 

To get them set properly, you will have to move lines 117-146 to above line 19, but that will probably break other stuff.

 

The fact of the matter is.... you need to understand what it is your trying to do so you can do it effectively. Maybe move lines 19 etc to below the lines 117-146 block.

 

P.S. I am donating my time here... so your urgency is not of my concern. If you want to pay me, then I will make it priority, but as it stands.... you get what you get when people are donating their time.

 

Hope this helps.

Archived

This topic is now archived and is closed to further replies.

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