Help please

Hey, so basically i have to make a website and link it up to a database, so that whenever the user puts information into the payment form and hits submit, it will send and insert into the database. I’ve got that set up, but it dosnt seem to add to the database. can someone help? I will put the code below. Thanks.

[php]

<?php $username = "student"; $password = "student"; $hostname = "localhost"; // connecting to the database $db = mysql_connect($hostname,$username,$password); if (!$db) { die("Connection Failed: " . mysql_error()); } if ($db) { echo "

Database Connection Status : Successful

"; } // selecting the database $db_select = mysql_select_db('antiques', $db); if (!$db_select) { die ("Failed to select database : " . mysql_error()); } if (($_SERVER['REQUEST_METHOD'] == 'POST') AND (isset($_POST['submit'] ))){ $chosen = $_POST['submit']; $FirstName = check($_POST['FirstName'],"Enter your FirstName"); $LastName = check($_POST['LastName'],"Enter your LastName"); $StreetAddress1 = check($_POST['StreetAddress1'],"Enter first street address"); $StreetAddress2 = check($_POST['StreetAddress2'],"Enter your second street address"); $TownCity = check($_POST['Town/City'],"Enter your Town/City"); $Postcode = check($_POST['Postcode'],"Enter your Postcode"); $CreditCardNo = check($_POST['CreditCardNo'],"Enter your Credit Card No"); $SecurityNo = check($_POST['SecurityNo'],"Enter your Security No"); $sql="INSERT INTO antique (`FirstName`, `LastName`, `Street Address1`, `Street Address2`, `Town/City`, `Postcode`, `Credit Card No`, `Security No`) VALUES ('{$FirstName}','{$LastName}','{$StreetAddress1}','{$StreetAddress2}','{$TownCity}','{$Postcode}','{$CreditCardNo}','{$SecurityNo}')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } else { echo "1 record added"; echo 'Hello ' . htmlspecialchars ( $_POST [ "FirstName" ]). ' '; echo htmlspecialchars ( $_POST [ "LastName" ]). '!
' ; } mysql_close($db); } ?>
        <?php

function check( $value , $problem="") {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists( “mysql_real_escape_string” );
if( $new_enough_php ) {
if( $magic_quotes_active ) { $value = stripslashes( $value ); }
$value = mysql_real_escape_string( $value );
} else {
if( !$magic_quotes_active ) { $value = addslashes( $value ); }
}
if ($problem && strlen($value) == 0){
die($problem);
}
return $value;
}
?>

[/php]

I changed the code, now it looks like this but still wont send to the db. why??

<div class="container">
  <div class="sidebar1">
    <ul class="nav">
      <li><a href="Homepage.php">Homepage</a></li>
      <li><a href="ContactUs.php">Contact Us</a></li>
      <li><a href="AboutUs.php"> About Us</a></li>
      <li><a href="Login.php">Log in</a></li>
    </ul>
    <p>&nbsp;</p>
    <!-- end .sidebar1 -->
  </div>
  <div class="content">
    <h1>Welcome to McKinney's Antiques</h1>
    <p>Welcome to McKinney's Antiques, where you'll find the best  antiques at the best prices!.</p>
    <p>At McKinney's Antiques, you can be guaranteed quality,  espically when it comes to price.</p>
    <p><strong>	Payment Page...</strong></p>
    <p>	This  is our main Payment page for purchasing items &amp; ordering. We accept all  major Credit Cards. Please make sure you have entered the correct information,  as the items will be shipped to the address entered below. We accept all major credit cards.</p>
    
    <p></p>
    <form action="Payment.php" method="post" name="Payment">

    <p>Please pick your product: <select name="dropdownbox">
<option value="Clock1">Clock 1</option>
<option value="Clock2">Clock 2</option>
<option value="Clock3">Clock 3</option>
<option value="Cutlery1">Cutlery 1</option>
<option value="Cutlery2">Cutlery 2</option>
<option value="Cultery3">Cutlery 3</option>
<option value="Furniture1">Furniture 1</option>
<option value="Furniture2">Furniture 2</option>
<option value="Furniture3">Furniture 3</option>

</select>
  <p>	FirstName: <input type="text" name="Firstname" value="" /></p>
       <p>	LastName: <input type="text" name="LastName" value=""  /></p>
          <p>	Street Address 1: <input type="text" name="Street Address1"  value="" /></p>
             <p>	Street Address 2: <input type="text"name="Street Address2"  value="" /></p>
                <p>	Town/City: <input type="text" name="Town/City"  value="" /></p>
                   <p>	Postcode: <input type="text" name="Postcode" value="" /></p>
                   <p>	Credit Card No: <input type="text" name="Credit CardNo" value="" /></p>
                   <p>	Security No: <input type="text" name="SecurityNo" value="" /></p>
          <div align="center"><input type="submit" value="Submit Form" />
<input type="reset" value="Reset Form" />
</form>
</FORM></p>

[php]

<?php // connecting to the database $link = mysql_connect('localhost','student','student') or die(mysql_error()); mysql_select_db('antiques',$link); if (!$link) { die("Connection Failed: " . mysql_error()); } if ($link) { echo "

Database Connection Status : Successful

"; } // selecting the database $db_select = mysql_select_db('antiques', $link); if (!$db_select) { die ("Failed to select database : " . mysql_error()); $FirstName = check($_POST['FirstName'],"Enter your FirstName"); $LastName = check($_POST['LastName'],"Enter your LastName"); $StreetAddress1 = check($_POST['StreetAddress1'],"Enter first street address"); $StreetAddress2 = check($_POST['StreetAddress2'],"Enter your second street address"); $TownCity = check($_POST['Town/City'],"Enter your Town/City"); $Postcode = check($_POST['Postcode'],"Enter your Postcode"); $CreditCardNo = check($_POST['CreditCardNo'],"Enter your Credit Card No"); $SecurityNo = check($_POST['SecurityNo'],"Enter your Security No"); $query="INSERT INTO customer VALUES (NULL,'$FirstName','$LastName','$StreetAddress1','$StreetAddress2','$TownCity','$Postcode','$CreditCardNo','$SecurityNo')"; $result = mysql_query($query,$link) or die(mysql_error()); $affected = mysql_affected_rows($link); if ($affected >0) {echo "success";} else {echo "fail";} } ?>

[/php]

Your insert query is inside the (!$db_select) condition. You need to add a closing bracket before $FirstName and remove the one on the last line.

Hey, thanks for the reply. I dont as you said, but it dosnt still send to the database. Is it something to do with my html code? or php?

<div class="container">
  <div class="sidebar1">
    <ul class="nav">
      <li><a href="Homepage.php">Homepage</a></li>
      <li><a href="ContactUs.php">Contact Us</a></li>
      <li><a href="AboutUs.php"> About Us</a></li>
      <li><a href="Login.php">Log in</a></li>
    </ul>
    <p>&nbsp;</p>
    <!-- end .sidebar1 -->
  </div>
  <div class="content">
    <h1>Welcome to McKinney's Antiques</h1>
    <p>Welcome to McKinney's Antiques, where you'll find the best  antiques at the best prices!.</p>
    <p>At McKinney's Antiques, you can be guaranteed quality,  espically when it comes to price.</p>
    <p><strong>	Payment Page...</strong></p>
    <p>	This  is our main Payment page for purchasing items &amp; ordering. We accept all  major Credit Cards. Please make sure you have entered the correct information,  as the items will be shipped to the address entered below. We accept all major credit cards.</p>
    
    <p></p>
    <form action="Payment.php" method="post" name="Payment">

    <p>Please pick your product: <select name="dropdownbox">
<option value="Clock1">Clock 1</option>
<option value="Clock2">Clock 2</option>
<option value="Clock3">Clock 3</option>
<option value="Cutlery1">Cutlery 1</option>
<option value="Cutlery2">Cutlery 2</option>
<option value="Cultery3">Cutlery 3</option>
<option value="Furniture1">Furniture 1</option>
<option value="Furniture2">Furniture 2</option>
<option value="Furniture3">Furniture 3</option>

</select>
  <p>	FirstName: <input type="text" name="Firstname" value="" /></p>
       <p>	LastName: <input type="text" name="LastName" value=""  /></p>
          <p>	Street Address 1: <input type="text" name="Street Address1"  value="" /></p>
             <p>	Street Address 2: <input type="text"name="Street Address2"  value="" /></p>
                <p>	Town/City: <input type="text" name="Town/City"  value="" /></p>
                   <p>	Postcode: <input type="text" name="Postcode" value="" /></p>
                   <p>	Credit Card No: <input type="text" name="Credit CardNo" value="" /></p>
                   <p>	Security No: <input type="text" name="SecurityNo" value="" /></p>
          <div align="center"><input type="submit" name="Submit" value="Submit Form" />
<input type="reset" value="Reset Form" />
</form>
</FORM></p>

[php]

<?php // connecting to the database $link = mysql_connect('localhost','student','student') or die(mysql_error()); mysql_select_db('antiques',$link); if (!$link) { die("Connection Failed: " . mysql_error()); } if ($link) { echo "

Database Connection Status : Successful

"; } // selecting the database $db_select = mysql_select_db('antiques', $link); if (!$db_select) { die ("Failed to select database : " . mysql_error()); } $FirstName ($_POST['FirstName'],"Enter your FirstName"); $LastName ($_POST['LastName'],"Enter your LastName"); $StreetAddress1($_POST['StreetAddress1'],"Enter first street address"); $StreetAddress2 ($_POST['StreetAddress2'],"Enter your second street address"); $TownCity ($_POST['Town/City'],"Enter your Town/City"); $Postcode ($_POST['Postcode'],"Enter your Postcode"); $CreditCardNo ($_POST['CreditCardNo'],"Enter your Credit Card No"); $SecurityNo ($_POST['SecurityNo'],"Enter your Security No"); $query="INSERT INTO customer VALUES (NULL,'$FirstName','$LastName','$StreetAddress1','$StreetAddress2','$TownCity','$Postcode','$CreditCardNo','$SecurityNo')"; $result = mysql_query($query,$link) or die(mysql_error()); $affected = mysql_affected_rows($link); if ($affected >0) {echo "success";} else {echo "fail";} ?>

[/php]

When thats done, and when i try and run it, it says;

Notice: Undefined variable: FirstName in C:\wamp\www\Website\Payment.php on line 193

AND

Fatal error: Function name must be a string in C:\wamp\www\Website\Payment.php on line 193

Any ideas how to fix this? Im new to PHP so yeah ive obviously done something weird. Thanks.

Well this isn’t valid code. You are missing = after your variable definition and I’m not sure if you intended to use some kind of error reporting function?

[php]
$FirstName ($_POST[‘FirstName’],“Enter your FirstName”);
$LastName ($_POST[‘LastName’],“Enter your LastName”);
[/php]

You also have inconsistencies in your input names vs $_POST variables e.g.

“Firstname” is not $_POST[‘FirstName’]
“Street Address1” is not $_POST[‘StreetAddress1’]

These must be identical (including case)

Here is a complete example for you, with some additional notes

[php]

<?php // function to check request variables and populate errors array function get_request_var($var, $error_msg, &$errors) { if (isset($_REQUEST[$var])) { // request var found return $_REQUEST[$var]; // note sanitizing can be done here as well e.g. addslashes } $errors[] = $error_msg; // add error message return false; // assign false to variable } // connecting to the database $link = mysql_connect('localhost','student','student') or die(mysql_error()); // check link BEFORE mysql_select_db if (!$link) { die("Connection Failed: " . mysql_error()); } // mysql_select_db('antiques',$link); // database is selected below // this code is not necessary - if it hasn't failed then it's successful if ($link) { echo "

Database Connection Status : Successful

"; } // selecting the database $db_select = mysql_select_db('antiques', $link); if (!$db_select) { die ("Failed to select database : " . mysql_error()); } // define errors array $errors = array(); // check request variables $FirstName = get_request_var('FirstName', "Enter your FirstName", $errors); $LastName = get_request_var('LastName', "Enter your LastName", $errors); $StreetAddress1 = get_request_var('StreetAddress1', "Enter first street address", $errors); $StreetAddress2 = get_request_var('StreetAddress2', "Enter your second street address", $errors); $TownCity = get_request_var('Town/City', "Enter your Town/City", $errors); $Postcode = get_request_var('Postcode', "Enter your Postcode", $errors); $CreditCardNo = get_request_var('CreditCardNo', "Enter your Credit Card No", $errors); $SecurityNo = get_request_var('SecurityNo', "Enter your Security No", $errors); // check for errors if (!$errors) { // do your insert $query="INSERT INTO customer VALUES (NULL,'$FirstName','$LastName','$StreetAddress1','$StreetAddress2','$TownCity','$Postcode','$CreditCardNo','$SecurityNo')"; $result = mysql_query($query,$link) or die(mysql_error()); $affected = mysql_affected_rows($link); if ($affected > 0) { echo "success"; } else { echo "fail"; } } else { // errors were found foreach($errors as $error) { echo $error . "
"; } } ?>

[/php]

i put that code in but it still wont go into the database. Why is this?

<div class="container">
  <div class="sidebar1">
    <ul class="nav">
      <li><a href="Homepage.php">Homepage</a></li>
      <li><a href="ContactUs.php">Contact Us</a></li>
      <li><a href="AboutUs.php"> About Us</a></li>
      <li><a href="Login.php">Log in</a></li>
    </ul>
    <p>&nbsp;</p>
    <!-- end .sidebar1 -->
  </div>
  <div class="content">
    <h1>Welcome to McKinney's Antiques</h1>
    <p>Welcome to McKinney's Antiques, where you'll find the best  antiques at the best prices!.</p>
    <p>At McKinney's Antiques, you can be guaranteed quality,  espically when it comes to price.</p>
    <p><strong>	Payment Page...</strong></p>
    <p>	This  is our main Payment page for purchasing items &amp; ordering. We accept all  major Credit Cards. Please make sure you have entered the correct information,  as the items will be shipped to the address entered below. We accept all major credit cards.</p>
    
    <p></p>
    <form action="Payment.php" method="post" name="Payment">

    <p>Please pick your product: <select name="dropdownbox">
<option value="Clock1">Clock 1</option>
<option value="Clock2">Clock 2</option>
<option value="Clock3">Clock 3</option>
<option value="Cutlery1">Cutlery 1</option>
<option value="Cutlery2">Cutlery 2</option>
<option value="Cultery3">Cutlery 3</option>
<option value="Furniture1">Furniture 1</option>
<option value="Furniture2">Furniture 2</option>
<option value="Furniture3">Furniture 3</option>

</select>
  <p>	FirstName: <input type="text" name="Firstname" value="" /></p>
       <p>	LastName: <input type="text" name="LastName" value=""  /></p>
          <p>	Street Address 1: <input type="text" name="Street Address1"  value="" /></p>
             <p>	Street Address 2: <input type="text"name="Street Address2"  value="" /></p>
                <p>	Town/City: <input type="text" name="Town/City"  value="" /></p>
                   <p>	Postcode: <input type="text" name="Postcode" value="" /></p>
                   <p>	Credit Card No: <input type="text" name="Credit CardNo" value="" /></p>
                   <p>	Security No: <input type="text" name="SecurityNo" value="" /></p>
          <div align="center"><input type="submit" name="Submit" value="Submit Form" />
<input type="reset" value="Reset Form" />
</form>
</FORM></p>

[php]

<?php // function to check request variables and populate errors array function get_request_var($var, $error_msg, &$errors) { if (isset($_REQUEST[$var])) { // request var found return $_REQUEST[$var]; // note sanitizing can be done here as well e.g. addslashes } $errors[] = $error_msg; // add error message return false; // assign false to variable } // connecting to the database $link = mysql_connect('localhost','student','student') or die(mysql_error()); // check link BEFORE mysql_select_db if (!$link) { die("Connection Failed: " . mysql_error()); } // mysql_select_db('antiques',$link); // database is selected below // this code is not necessary - if it hasn't failed then it's successful if ($link) { echo "

Database Connection Status : Successful

"; } // selecting the database $db_select = mysql_select_db('antiques', $link); if (!$db_select) { die ("Failed to select database : " . mysql_error()); } // define errors array $errors = array(); // check request variables $FirstName = get_request_var('FirstName', "Enter your FirstName", $errors); $LastName = get_request_var('LastName', "Enter your LastName", $errors); $StreetAddress1 = get_request_var('StreetAddress1', "Enter first street address", $errors); $StreetAddress2 = get_request_var('StreetAddress2', "Enter your second street address", $errors); $TownCity = get_request_var('Town/City', "Enter your Town/City", $errors); $Postcode = get_request_var('Postcode', "Enter your Postcode", $errors); $CreditCardNo = get_request_var('CreditCardNo', "Enter your Credit Card No", $errors); $SecurityNo = get_request_var('SecurityNo', "Enter your Security No", $errors); // check for errors if (!$errors) { // do your insert $query="INSERT INTO customer VALUES (NULL,'$FirstName','$LastName','$StreetAddress1','$StreetAddress2','$TownCity','$Postcode','$CreditCardNo','$SecurityNo')"; $result = mysql_query($query,$link) or die(mysql_error()); $affected = mysql_affected_rows($link); if ($affected > 0) { echo "success"; } else { echo "fail"; } } else { // errors were found foreach($errors as $error) { echo $error . "
"; } } ?>

[/php]

Well what does it output?

Did you update your form input names as well?

Hey so i changed my code some. Here it is below. But it keeps saying undefined index for every single variable. i dont know how to fix this. Other errors come up aswell, saying ;

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\wamp\www\Website\Payment.php on line 200

AND

Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\wamp\www\Website\Payment.php on line 202

My code is listed below. PLEASE HELP, im so stressed out over all this. Can you fix this code for me? and tell me what caused it so i can learn? Thanks.

  </div>
  <div class="content">
    <h1>Welcome to McKinney's Antiques</h1>
    <p>Welcome to McKinney's Antiques, where you'll find the best  antiques at the best prices!.</p>
    <p>At McKinney's Antiques, you can be guaranteed quality,  espically when it comes to price.</p>
    <p><strong>	Payment Page...</strong></p>
    <p>	This  is our main Payment page for purchasing items &amp; ordering. We accept all  major Credit Cards. Please make sure you have entered the correct information,  as the items will be shipped to the address entered below. We accept all major credit cards.</p>
    
    <p></p>
    <form action="Payment.php" method="post" name="Payment">

    <p>Please pick your product: <select name="dropdownbox">
<option value="Clock1">Clock 1</option>
<option value="Clock2">Clock 2</option>
<option value="Clock3">Clock 3</option>
<option value="Cutlery1">Cutlery 1</option>
<option value="Cutlery2">Cutlery 2</option>
<option value="Cultery3">Cutlery 3</option>
<option value="Furniture1">Furniture 1</option>
<option value="Furniture2">Furniture 2</option>
<option value="Furniture3">Furniture 3</option>

</select>
  <p>	FirstName: <input type="text" name="Firstname" value="" /></p>
       <p>	LastName: <input type="text" name="LastName" value=""  /></p>
          <p>	Street Address 1: <input type="text" name="Street Address1"  value="" /></p>
             <p>	Street Address 2: <input type="text"name="Street Address2"  value="" /></p>
                <p>	Town/City: <input type="text" name="Town/City"  value="" /></p>
                   <p>	Postcode: <input type="text" name="Postcode" value="" /></p>
                   <p>	Credit Card No: <input type="text" name="Credit CardNo" value="" /></p>
                   <p>	Security No: <input type="text" name="SecurityNo" value="" /></p>
          <div align="center"><input type="submit" name="Submit" value="Submit Form" />
<input type="reset" value="Reset Form" />
</form>
</FORM></p>

[php]

<?php $username = "student"; $password = "student"; $hostname = "localhost"; //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "Connected to MySQL
"; ?> <?php //select a database to work with $selected = mysql_select_db("antiques",$dbhandle) or die("Could not select antiques"); //insert into database $sql="INSERT INTO customers (FirstName, LastName, StreetAddress1, StreetAddress2, TownCity, Postcode, CreditCardNo, SecurityNo) VALUES ('$_POST[FirstName]','$_POST[LastName]','$_POST[StreetAddress1]','$_POST[StreetAddress2]','$_POST[TownCity]','$_POST[Postcode]','$_POST[CreditCardNo]','$_POST[SecurityNo]')"; if (isset($_POST['FirstName'])) { $selected = $_POST['FirstName']; } if (!mysqli_query($selected,$sql)) { die('Error: ' . mysqli_error($selected)); } echo "1 record added"; mysqli_close($con); ?>

[/php]

You can’t mix mysql_* functions with mysqli_* functions

Sponsor our Newsletter | Privacy Policy | Terms of Service