如何将 HTML 表单插入 MySQL 数据库? [英] How do I insert an HTML form into a MySQL Database?

查看:76
本文介绍了如何将 HTML 表单插入 MySQL 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个计费系统,它现在非常基础,但我想知道您是否可以帮助我处理我的代码.如何将 HTML 表单中的数据插入 MySQL 数据库?我的代码:

I am working on a billing system,, It's very basic right now but I was wondering if you could help me with my code. How do I insert the data from the HTML form into a MySQL database? My code:

<?php

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

    include('connect-mysql.php');

    $date = $_POST['date'];
    $charge = $_POST['charge'];
    $payment = $_POST['payment'];
    $client_no = $_POST['client_no'];
    $client_name = $_POST['client_name'];
    $check_no = $_POST['check_no'];
    $check = $_POST['check'];
    $cash = $_POST['cash'];
    $notes = $_POST['notes'];
    $staff_initials = $_POST['staff_initials'];
    $sqlinsert = "INSERT INTO payments (date, charge, payment, client_no, client_name, check_no, check, cash, notes, staff_initials) VALUES ('$date', '$charge', 'payment', '$client_no', '$client_name', '$check_no', '$check', '$cash', '$notes', '$staff_initials')";

    if (!mysqli_query($dbcon, $sqlinsert)) {
        die('There was an error when trying to process your payment. Please contact technical support.');
    } //end of nested if statement

} // end of the main if statement
?>

<html>
<head>
    <title>New Payment</title>
</head>
<body>
<h1>Please Input Payment Details</h1>
<form action="new_payment.php" method="POST">
<input type="hidden" name="submitted" value="true" />
<fieldset>
    <legend>New Payment</legend>
    <label>Date:<input type="text" name="date" /></label><br>
    <label>Today's Charge: <input type="text" name="charge" /></label><br>
    <label>Today's Payment: <input type="text" name="payment" /></label><br>
    <label>Client Number: <input type="text" name="client_no" /></label><br>
    <label>Client Name: <input type="text" name="client_name" /></label><br>
    <label>Check Number: <input type="text" name="check_no" /></label><br>
    <label>Check: <input type="text" name="check" /></label><br>
    <label>Cash: <input type="text" name="cash" /></label><br>
    <label>Notes: <input type="text" name="notes" /></label><br>
    <label>Staff Initials: <input type="text" name="staff_initials" /></label><br>
</fieldset>
<br />
<input type="submit" value="Process Payment">
</form>
</body>
</html>

请帮助我,这对我及时完成非常重要...

Please help me, this is very important that I finish this in a timely manner...

推荐答案

第一个错误:-

if (isset($_POST['submitted']))

<input type="submit" value="Process Payment">

改成这样:-

if($_POST['process'] == 'Process Payment') 
<input name="process" type="submit" id="process" value="Process Payment">

你可以用这个代替(防止 SQL 注入):

You can use this one instead (to prevent SQL injection):

// CONNECTION TO DATABASE

$mysqli = new mysqli('HOST','USERNAME','PASSWORD','DATABASE'); 

// CHECK CONNECTION

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}        

$sqlinsert = "INSERT INTO payments (date, charge, payment, client_no, client_name, check_no, check, cash, notes, staff_initials) VALUES (?,?,?,?,?,?,?,?,?,?)";

$stmt = $mysqli->prepare($sqlinsert);

    $date = $_POST['date'];
    $charge = $_POST['charge'];
    $payment = $_POST['payment'];
    $client_no = $_POST['client_no'];
    $client_name = $_POST['client_name'];
    $check_no = $_POST['check_no'];
    $check = $_POST['check'];
    $cash = $_POST['cash'];
    $notes = $_POST['notes'];
    $staff_initials = $_POST['staff_initials'];

$stmt->bind_param("ssssssssss",$date, $charge, $payment, $client_no, $client_name, $check_no, $check, $cash, $notes, $staff_initials );

//EXECUTE QUERY
$stmt->execute();

$rowcount = $stmt->affected_rows;


if ($rowcount > 0)
{
echo "Success!";
}

else
{
echo "Error!";
}

//CLOSE EXECUTE
$stmt->close();

这篇关于如何将 HTML 表单插入 MySQL 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆