简体   繁体   中英

Submitting values to database from form

I have a billing page which fetches data from the cart.

I am having a problem submitting these values to the database. How do i correct my php to get it to submit to the database correctly?

For example, i have a transactions table in my database, and i want all payment details to go there, which can later be fetched and outputted in the admin section as a receipt of order.

I cannot get the name, address, email, phone to submit correctly. It comes up with 0 in the database, how do i fix this?

How can i specifically use AJAX when the button is clicked to show a loading bar/page and then the success message?

I also get the error:

Notice: Undefined index: command in F:\xamppnew\htdocs\web\billing.php on line 5

Code:

    <?php
include "storescripts/connect_to_mysql.php"; 
session_start(); 

if($_REQUEST['command']=='update'){
        $name=$_REQUEST['name'];
        $email=$_REQUEST['email'];
        $address=$_REQUEST['address'];
        $phone=$_REQUEST['phone'];
        $item_id = $_SESSION['item_id'];
        $quantityorder = $each_item['quantity'] = $_SESSION['quantity1'];
        $cartTotal = $_SESSION['cartTotal'];

    // Add this product into the database now
    $sql = mysqli_query($link,"insert into transactions values('','$item_id','$quantityorder','$cartTotal','$name','$address','$email','$phone')");

        die('Thank You! your order has been placed!');
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
<title>Billing Info</title>
<script language="javascript">
    function validate(){
        var f=document.form1;
        if(f.name.value==''){
            alert('Your name is required');
            f.name.focus();
            return false;
        }
        f.command.value='update';
        f.submit();
    }
</script>
</head>



<body>
<div align="center" id="mainWrapper">
  <?php include_once("template_header.php");?>
  <div id="pageContent">
    <div style="margin:24px; text-align:left;">

    <br />
<form name="form1" onsubmit="return validate()">
    <input type="hidden" name="command" />
    <div align="center">
        <h1 align="center">Billing Info</h1>
        <table border="0" cellpadding="2px">
             <tr><td>Product ID:</td><td><?php echo $item_id = $_SESSION['item_id'];?></td></tr>
            <tr><td>Total Quantity:</td><td><?php echo $each_item['quantity'] = $_SESSION['quantity1']; ?></td></tr>
            <tr><td>Order Total:</td><td>£<?php echo $cartTotal = $_SESSION['cartTotal'];?></td></tr>
            <tr><td>Your Name:</td><td><input type="text" name="name" /></td></tr>
            <tr><td>Address:</td><td><input type="text" name="address" /></td></tr>
            <tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
            <tr><td>Phone:</td><td><input type="text" name="phone" /></td></tr>
            <tr><td>&nbsp;</td><td><input type="submit" value="Place Order" /></td></tr>
        </table>
    </div>
    </div>


      <?php include_once("template_footer.php");?>

</form>
</body>
</html>

It is a good practice to use addslashes over your values before concat them in an sql query

    $name    = addslashes($_REQUEST['name']);
    $email   = addslashes($_REQUEST['email']);
    $address = addslashes($_REQUEST['address']);
    $phone   = addslashes($_REQUEST['phone']);

did you use Mysqli to connect ? if yes, change mysql_query to mysqli_query

and you need to add 'name' attribute to the submit button and do this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM