방금 데이터를 PHP로, PHP를 데이터베이스로 보내는 ajax 함수를 만들었습니다. dotp_task_log 테이블에 삽입하면 정상적으로 작동합니다. 그러나 또한 dotp_task_log에 추가 한 후 dotp_tasks에 데이터를 추가해야 할 때 추가되지 않고 이유를 찾을 수 없습니다. Gerror가 발생합니다. 여기에 데이터베이스에 데이터를 추가하는 PHP 파일이 있습니다.
<?php
$currentUser = isset($_POST['currentUser']) ? $_POST['currentUser'] : '';
$currentTasken = isset($_POST['currentTasken']) ? $_POST['currentTasken'] : '';
$currentPercent = isset($_POST['currentPercent']) ? $_POST['currentPercent'] : '';
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
if(!$con)
die('Could not connectzzz: ' . mysql_error());
mysql_select_db("foxi" , $con) or die ("could not load the database" . mysql_error());
$check = mysql_query("SELECT * FROM dotp_task_log");
$numrows = mysql_num_rows($check);
if($numrows >= 1)
{
//$pass = md5($pass);
$ins = mysql_query("INSERT INTO dotp_task_log (task_log_creator, task_log_Task) VALUES ('$currentUser' , '$currentTasken')" ) ;
if($ins)
{
$check = mysql_query("SELECT * FROM dotp_tasks");
$numrows = mysql_num_rows($check);
if($numrows > 1)
{
//$pass = md5($pass);
$inss = mysql_query("INSERT INTO dotp_tasks (task_percent_complete) VALUES ('$currentPercent') WHERE task_id='$currentTasken'" ) ;
if($inss)
{
die("Succesfully added Percent!");
}
else
{
die("GERROR");
}
}
else
{
die("Log already exists!");
}
}
else
{
die("ERROR");
}
}
else
{
die("Log already exists!");
}
?>
코멘트에서 언급했듯이 :
INSERT ...
WHERE
절이 없습니다 . 오류 검사 는 구문 오류를 나타냅니다. INSERT ON DUPLICATE KEY 는 수행합니다. 대신 UPDATE를 사용하고 싶었을 수 있습니다.
$inss = mysql_query("UPDATE dotp_tasks
SET task_percent_complete = '$currentPercent'
WHERE task_id='$currentTasken'" );
참조 :
또한 테스트 할 때 오류 검사를 사용하십시오.
사용자 지정 메시지를 에코하는 대신.
추가 or die(mysql_error())
에 mysql_query()
.
파일 상단에 오류보고 를 추가하면 오류 를 찾는 데 도움이됩니다.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
참고 : 오류보고는 스테이징에서만 수행해야하며 프로덕션에서는 수행하지 않아야합니다.
각주 :
HTML 양식에 POST 메소드가 있고 모든 입력에 이름 속성이 있고 오타가 없는지 확인하십시오. 오류보고를 사용하면이를 알릴 수 있습니다.
현재 코드는 SQL 주입에 열려 있습니다. 사용 mysqli
준비된 명령문으로 , 또는 준비된 명령문과 PDO , 그들은 훨씬 더 안전있어 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다