Jump to content

PHP Insert Into Help


richo89

Recommended Posts

Hey I currently have a form whereby user fills in all the required details and clicks submit which sends it to mySQL database.

 

However one of the fields I want to be filled in dependent on which car the user is buying.

 

So from my code you can see I have obtained the carNo which is unique to each car. Now im trying to insert that into mySQL table when the user clicks submit.

 

The code I have is as follows

 

<?
//Grabbing the varialb carNo which is UNIQUE from carSearch.php
$carNo = $_GET['carNo']; 

echo "Car Number is $carNo"

   //change user and password to your mySQL name and password
   mysql_connect("localhost","root",""); 

   //select which database you want to edit
   mysql_select_db("cars"); 

   //convert all the posts to variables:
   $title = $_POST['Title'];
   $first_name = $_POST['First_name'];
   $surname = $_POST['Surname'];
   $telephone = $_POST['Telephone'];
   $address1 = $_POST['Address1'];
   $address2 = $_POST['Address2'];
   $city = $_POST['City'];
   $postcode = $_POST['Postcode'];
   $email = $_POST['Email'];
   $password = $_POST['Password'];
   $cardtype = $_POST['CardType'];
   $cardno = $_POST['CardNumber'];
   $cardexpiry = $_POST['CardExpiry'];
   $carNo = $_GET['carNo']; 
   

   
   //VALIDATION HERE
   if($first_name==''){
echo	"Please enter firstname<br>";
}
if($surname==''){
echo	"Please enter your surname<br>";
}
if($telephone==''){
echo	"Please enter a contact telephone number<br>";
}
if($address1==''){
echo	"Please enter the 1st line of address<br>";
}
if($address2==''){
echo	"Please enter the 2nd line of address<br>";
}
if($city==''){
echo	"Please enter the City<br>";
}
if($postcode==''){
echo	"Please enter the Postcode<br>";
}
if($email==''){
echo	"Please enter your e-mail address<br>";
}
if($password==''){
echo	"Please enter a password<br>";
}
if($cardno==''){
echo	"Please enter your Card Number<br>";
}
if($cardexpiry==''){
echo	"Please enter your Card Expiry Date<br>";
}

else{

//VALIDATION HERE

   
   //Insert the values into the correct database with the right fields
   //mysql table = purchases
   $result=MYSQL_QUERY("INSERT INTO purchases (Title,First_name,Surname,Telephone,Address1,Address2,City,Postcode,Email,Password,Card_type,Card_no,Card_expiry,CarNo)".
      "VALUES ('$title', '$first_name', '$surname', '$telephone', '$address1', '$address2', '$city', '$postcode', '$email', '$password', '$cardtype', '$cardno', '$cardexpiry' , '$carNo')"); 

    //confirm
echo "Registration Complete";
echo "echo "<meta HTTP-EQUIV='refresh' content=2;url='buyConfirm.php'>";
}
?>

 

As you can see in my Inset Into query I have $carNo but it justs enters it as 0 into my database.

 

Any ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/152983-php-insert-into-help/
Share on other sites

Sorry my initial post I meant I had tried removing the 2nd $carNo with no luck..

 

The URL i am passing is from blank.php and looks like the following:

 

echo "<td>";
echo "<td align=right bgcolor=$bgcolor id='title'> <font face='Verdana' size='4'><a href='buy.php?carNo=".$noticia['carNo']."'>Buy Now!</a></font></td>";  
echo "</td>";

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/152983-php-insert-into-help/#findComment-803501
Share on other sites

Change

 

$result=MYSQL_QUERY("INSERT INTO purchases (Title,First_name,Surname,Telephone,Address1,Address2,City,Postcode,Email,Password,Card_type,Card_no,Card_expiry,CarNo)".

      "VALUES ('$title', '$first_name', '$surname', '$telephone', '$address1', '$address2', '$city', '$postcode', '$email', '$password', '$cardtype', '$cardno', '$cardexpiry' , '$carNo')");

 

 

to

 

$sql=("INSERT INTO purchases (Title,First_name,Surname,Telephone,Address1,Address2,City,Postcode,Email,Password,Card_type,Card_no,Card_expiry,CarNo)".

      "VALUES ('$title', '$first_name', '$surname', '$telephone', '$address1', '$address2', '$city', '$postcode', '$email', '$password', '$cardtype', '$cardno', '$cardexpiry' , '$carNo')");

 

echo $sql;

 

$result = mysql_query($sql) or die (mysql_error());

 

And post what it echos.

Link to comment
https://forums.phpfreaks.com/topic/152983-php-insert-into-help/#findComment-803511
Share on other sites

Results as follows:

 

INSERT INTO purchases (Title,First_name,Surname,Telephone,Address1,Address2,City,Postcode,Email,Password,Card_type,Card_no,Card_expiry,CarNo)VALUES ('Mr', 'Test Name', 'Test Surname', '012222', 'Test Way', 'Test Road', 'Test City', 'TE2 TE4', '[email protected]', 'testingtest', 'Visa', '1234567890', '12/14' , '')

 

^^ Not placing anything into CarNo  :(

Link to comment
https://forums.phpfreaks.com/topic/152983-php-insert-into-help/#findComment-803517
Share on other sites

The whole code is as follows:

 


<?
//Grabbing the varialb carNo which is UNIQUE from carSearch.php
$carNo = $_GET['carNo']; 


echo "Car Number is $carNo";

require "config.php";           // All database details will be included here 

$page_name="buy.php"; //  If you use this code with a different page ( or file ) name then change this 

@$column_name=$_GET['column_name']; // Read the column name from query string. 


$start=$_GET['start'];								// To take care global variable if OFF
if(!($start > 0)) {                         // This variable is set to zero for the first page
$start = 0;
}

$eu = ($start - 0); 
$limit = 6;                                 // No of records to be shown per page.
$this1 = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 

/////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
$query2 = "SELECT * FROM cars WHERE carNo='".$carNo."'";  
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);

////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
$query = "SELECT * FROM cars WHERE carNo='".$carNo."'";  
if(isset($column_name) and strlen($column_name)>0){
$query = $query . " order by $column_name";
}
$query = $query. " limit $eu, $limit ";
$result=mysql_query($query);
echo mysql_error();


//Move the start table tag out of while loop
echo "<table border=0 width=100%>"; 
while($noticia = mysql_fetch_array($result)) 
{ 
if($bgcolor=='#ffffff'){$bgcolor='#ffffff';} 
else{$bgcolor='#ffffff';} 

echo "<table border=0 width=100%>";
echo "<tr >"; 
echo "<td align=left colspan=5 BGCOLOR=#F8F8F8 id='title'> <font face='Verdana' color='#A00000' size='3'> <a href='javascript:history.go(-1)'>Back to Car</a>    <center><b><u>$noticia[make] $noticia[model]</center></b></u></font></td>"; 
echo "<tr >"; 
echo "<td align=left rowspan=13 width=180 bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'><center><img src='$noticia[pictureL]'</font></center></td>"; 
echo "<td>   </td>";
echo "<tr >";
echo "<td align=left colspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' size='3'><i><u>Car Details:<font></u></i></td>"; 
echo "<tr >"; 
echo "<td align=left colspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'>Registration:$noticia[Reg]</font></td>";
echo "<tr >";
echo "<td align=left colspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'>Colour:$noticia[colour]</td>";
echo "<tr >";
echo "<td align=left colspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'>Mileage:$noticia[miles]</td>";
echo "<tr >";
echo "<td align=left colspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'>Description:$noticia[description]</td>";
echo "<tr >";
echo "<tr >";
echo "<td align=left colspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' size='3'><i><u>Location:<font></u></i></td>";
echo "<tr >";
echo "<td align=left colspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'>Dealer:$noticia[dealer]</font></td>";
echo "<tr >";
echo "<td align=left colspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'>Town:$noticia[town]</td>";
echo "<tr >";
echo "<td align=left colspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'>Region:$noticia[region]</td>";
echo "<tr >";
echo "<td align=center colspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' color='blue' size='3'><u>Telephone:$noticia[telephone]</u></font></td>";
echo "<tr >";
echo "<td align=center colspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' color='red' size='3'><u>Price: £$noticia[price]</u></font></td>";
echo "<tr >";
//Change 'More Info???' link to include the id passed to next page via GET
echo "<td>";
echo "</tr>"; 
} 
echo "</table>";


?>

</div>

<!-- Customer Registration Purchasing --!>
<div id="textcellNav" style="position: absolute; left: 310px; top:630px; width: 660px; height: 400px;">
<font size=5 color=	#A00000><center> Complete Purchase </font></center>

<form action="buy.php" method="POST">
<table border="0" bordercolor="" width="" bgcolor="">
<tr>
<td><b><u> Personal Information </b></u></td>
<td>  </td>
</tr>
<td> Title: </td>
<td> <select name="Title">
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Miss">Miss</option>
<option value="Ms">Ms</option>
</select> </td>
</tr>
<tr>
<td> First Name: </td>
<td> <input type="text" size="22" name="First_name"> </td>
</tr>
<tr>
<td> Surname: </td>
<td> <input type="text" size="22" name="Surname"> </td>
</tr>
<tr>
<td> Telephone: </td>
<td> <input type="text" size="22" name="Telephone"> </td>
</tr>
<tr>
<td> Address: </td>
<td> <input type="text" size="22" name="Address1"> </td>
</tr>
<tr>
<td>  </td>
<td> <input type="text" size="22" name="Address2"> </td>
</tr>
<tr>
<td> City: </td>
<td> <input type="text" size="22" name="City"> </td>
</tr>
<tr>
<td> Postcode: </td>
<td> <input type="text" size="22" name="Postcode"> </td>
</tr>
<tr>
<td> <b><u> Login Information </b></u></td>
<td>  </td>
</tr>
<tr>
<td> E-mail Address: </td>
<td> <input type="text" size="22" name="Email"> </td>
</tr>
<tr>
<td> Password: </td>
<td> <input type="password" size="22" name="Password"> </td>
</tr>
<tr>
<td> <b><u>Payment Information </b></u></td>
<td>  </td>
</tr>
<tr>
<td> Card Type </td>
<td> <select name="CardType">
<option value="Visa">Visa</option>
<option value="Visa Debit">Visa Debit</option>
<option value="Mastercard">Mastercard</option>
<option value="Maestro">Maestro</option>
</select> </td>
</tr>
<tr>
<td> Card Number </td>
<td> <input type="text" size="22" name="CardNumber"> </td>
</tr>
<tr>
<td> Expiry Date (MM/YY)</td>
<td> <input type="text" size="22" name="CardExpiry"> </td>
</tr>




</table>
<style type=text/css>
input.red {background-color: #A00000; font-weight: bold; font-size: 12px; color: white;}
</style>
<input class="red" type="submit" name="submit" value="Submit">
    <br>

<? 
//initilize PHP

session_start();



   //convert all the posts to variables:
   $title = $_POST['Title'];
   $first_name = $_POST['First_name'];
   $surname = $_POST['Surname'];
   $telephone = $_POST['Telephone'];
   $address1 = $_POST['Address1'];
   $address2 = $_POST['Address2'];
   $city = $_POST['City'];
   $postcode = $_POST['Postcode'];
   $email = $_POST['Email'];
   $password = $_POST['Password'];
   $cardtype = $_POST['CardType'];
   $cardno = $_POST['CardNumber'];
   $cardexpiry = $_POST['CardExpiry'];
   

   
   //VALIDATION HERE
   if($first_name==''){
echo	"Please enter firstname<br>";
}
if($surname==''){
echo	"Please enter your surname<br>";
}
if($telephone==''){
echo	"Please enter a contact telephone number<br>";
}
if($address1==''){
echo	"Please enter the 1st line of address<br>";
}
if($address2==''){
echo	"Please enter the 2nd line of address<br>";
}
if($city==''){
echo	"Please enter the City<br>";
}
if($postcode==''){
echo	"Please enter the Postcode<br>";
}
if($email==''){
echo	"Please enter your e-mail address<br>";
}
if($password==''){
echo	"Please enter a password<br>";
}
if($cardno==''){
echo	"Please enter your Card Number<br>";
}
if($cardexpiry==''){
echo	"Please enter your Card Expiry Date<br>";
}

else{

//VALIDATION HERE

   
   //Insert the values into the correct database with the right fields
   //mysql table = purchases
  $sql=("INSERT INTO purchases (Title,First_name,Surname,Telephone,Address1,Address2,City,Postcode,Email,Password,Card_type,Card_no,Card_expiry,CarNo)".
      "VALUES ('$title', '$first_name', '$surname', '$telephone', '$address1', '$address2', '$city', '$postcode', '$email', '$password', '$cardtype', '$cardno', '$cardexpiry' , '$carNo')");

echo $sql;

$result = mysql_query($sql) or die (mysql_error());


    //confirm
echo "Registration Complete";
echo "Car Number is $carNo";
//echo "<meta HTTP-EQUIV='refresh' content=2;url='buyConfirm.php'>";
}
?>

 

I just tried placing the code for the part where it was required.

 

But thats the whole code including my form.

Link to comment
https://forums.phpfreaks.com/topic/152983-php-insert-into-help/#findComment-803523
Share on other sites

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.