1

私は請求システムに取り組んでいます.今は非常に基本的なものですが、私のコードを手伝ってくれるかどうか疑問に思っていました. HTML フォームから MySQL データベースにデータを挿入するにはどうすればよいですか? 私のコード:

<?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>

私を助けてください、これは私がタイムリーにこれを終わらせることが非常に重要です...

4

3 に答える 3

2

最初のエラー:-

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 インジェクションを防ぐため):

// 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();
于 2012-09-25T03:14:30.670 に答える
0

よくわからないけど変えた方がいいと思う

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

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

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

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

INSERTコマンドで支払いに$を入れます

$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')";
于 2012-09-25T02:56:01.260 に答える
-1

データベースへの挿入に問題がある場合は、変更する必要があります

 $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')";

 $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')";

Insert Value で支払いの前に $ を入力するのを忘れました

于 2012-09-25T02:54:52.200 に答える