2012-09-26 2 views
2

내 코드에 무슨 문제가 있는지 말해주세요 !!!!HTML 폼의 데이터를 MySQL에 삽입하는 방법

새로운 payment.php

<!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" /> 
<title>Process New Payment</title> 
</head> 

<body> 
<h1>Please Input Payment Details</h1> 
<fieldset> 
    <legend>New Payment</legend> 
    <form action="process-payment.php" method="post" /> 
<table> 
<tr> 
<td>Date:</td><td><input type="date" name="date" /><br /></td> 
</tr> 
<tr> 
<td>Today's Charge:</td><td><input type="text" name="charge" /><br /></td> 
</tr> 
<tr> 
<td>Today's Payment:</td><td><input type="text" name="payment" /><br /></td> 
</tr> 
<tr> 
<td>Client Number:</td><td><input type="text" name="client_no" /><br /></td> 
</tr> 
<tr> 
<td>Client Name:</td><td><input type="text" name="client_name" /><br /></td> 
</tr> 
<tr> 
<td>Check Number:</td><td><input type="text" name="check_no" /><br /></td> 
</tr> 
<tr> 
<td>Check Amount:</td><td><input type="text" name="check" /><br /></td> 
</tr> 
<tr> 
<td>Cash Amount:</td><td><input type="text" name="cash" /><br /></td> 
</tr> 
<tr> 
<td>Notes:</td><td><input type="text" name="notes" /><br /></td> 
</tr> 
<tr> 
<td>Staff Initials:</td><td><input type="text" name="staff_initials" /><br /></td> 
</tr> 
</table> 
<input type="submit" value="Process Payment"> 
    </form> 
</fieldset> 
<br /> 
</body> 
</html> 

프로세스 payment.php는

<?php 

define('DB_NAME', 'DBNAME'); 
define('DB_USER', 'USERNAME'); 
define('DB_PASS', ''); 
define('DB_HOST', 'localhost'); 

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS); 

if (!$link) { 
    dir('There was a problem when trying to connect to the host. Please contact Tech Support. Error: ' . mysql_error());  
} 

$db_selected = mysql_select_db(DB_NAME, $link); 

if (!$link) { 
    dir('There was a problem when trying to connect to the database. Please contact Tech Support. Error: ' . mysql_error());  
} 

$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']; 

$sql = "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 (!mysql_query($sql)) { 
    die('Error: ' . mysql_error()); 
} 

?> 

내가 잘못이 무엇인지 모르지만 내가 프로세스 지불 누르면 오류가 발생합니다 :

Error: You have an error in your SQL syntax; check the manual that corresponds to your >MySQL server version for the right syntax to use near 'check, cash, notes, staff_initials) >VALUES ('2012-09-24', '$0.00', '$20.00', '46' at line 1

+0

'$ 20.00'이 (가) 데이터베이스의 정수로 설정되면 작동하지 않습니다. 데이터베이스를 볼 수 있습니다. – wesside

+0

가능한 복제 : http://stackoverflow.com/questions/12575444/how-do-i-insert-an-html-form-into-a-mysql-database/12575726 – Furry

+0

[HTML을 어떻게 삽입합니까? 양식을 MySQL 데이터베이스로?] (http://stackoverflow.com/questions/12575444/how-do-i-insert-an-html-form-into-a-mysql-database) – Alice

답변

1

CHECKMySQL reserved keyword입니다. 열 또는 표 식별자로 사용하려면 백틱으로 묶어야합니다.

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

스크립트는 SQL 삽입에 취약합니다. 최소한 입력 변수 각각에 대해 mysql_real_escape_string()으로 전화해야합니다.

// As in: 
$charge = mysql_real_escape_string($_POST['charge']); 
+1

좋은 캐롤 형. – wesside

+0

감사합니다. !!!! lol –

1

변화

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

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

에 그리고 당신이 사용하고있는 감가 상각 연결 코드를 사용하는 대신 MySQL을 PDO를 조회 지불 할 수 있습니다.

+0

필요 없음. 큰 따옴표로 묶인 바깥 쪽 문자열은 변수가 이스케이프되는 한 안쪽 따옴표 안의 변수를 올바르게 보간합니다. –

+0

정말! 당신은 매일 새로운 것을 배웁니다. :) – Chris

+0

PDO 구현을위한이 대답을보십시오. http://security.stackexchange.com/questions/34655/is-there-any-sql-injection-for-this-php-login-example/34668 # 34668 – David

0

모든 요소를 ​​에코하려고하면 일부 변수가 비어 있거나 비어있을 수 있습니다.

동일한 오류가 있습니다. 나는이 방법으로 해결했습니다

관련 문제