I have a view where the data of the clients are displayed from a table'clientttente ', displayed by a ng-repeat suffers from an array, and for each line that belongs to each client I have an 'add' button, this button must add each client to another table 'client', when I try this code, it generates this error: undefined property "code", Where is the error please!
我有一個視圖,其中客戶端的數據顯示在table'clientttente'中,由ng-repeat顯示的數據顯示,並且對於屬於每個客戶端的每一行,我有一個“添加”按鈕,此按鈕必須將每個客戶端添加到另一個表'client',當我嘗試此代碼時,它會生成此錯誤:undefined property“code”,錯誤請在哪里!
register.html
register.html
Code.Client | Nom | Prenom | Ajouter |
---|---|---|---|
{{x.CodeClient}} | {{x.NomClient}} | {{x.PrenomClient}} | Ajouter |
app.js
app.js
$scope.insertClient = function(){
if(confirm("Êtes-vous sûr?"))
{
$http.post(
"http://localhost/deb/insertClient.php",
{
'code' :$scope.code,
'nom' :$scope.nom,
'prenom' :$scope.prenom,
}
).success(function(data){
});
}
else
{
return false;
}
}
insertClient.php
insertClient.php
$connect = mysqli_connect("localhost", "root", "", "test");
$data = json_decode(file_get_contents("php://input"));
if(count($data) > 0)
{ $CodeClient=mysqli_real_escape_string($connect,$data->code);
$NomClient = mysqli_real_escape_string($connect, $data->nom);
$PrenomClient = mysqli_real_escape_string($connect, $data->prenom);
$query = "INSERT INTO client (NomClient,PrenomClient) VALUES
('$NomClient','$PrenomClient') WHERE CodeClient='$CodeClient'";
if(mysqli_query($connect, $query))
{
echo "The client has been successfully added";
}
else
{
echo 'Error';
}
}
?>
2 个解决方案
#1
3
There is no INSERT INTO ... WHERE syntax. What you want to do is UPDATE ..SET ..WHERE:
沒有INSERT INTO ... WHERE語法。你想要做的是UPDATE ..SET ..WHERE:
$query = "UPDATE client
SET NomClient ='$NomClient' ,PrenomClient = '$PrenomClient'
WHERE CodeClient='$CodeClient'";
And as I suggested in my comment:
正如我在評論中所說:
Use prepared statements not mysqli_real_escape_string to prevent SQL injection.
使用預處理語句而不是mysqli_real_escape_string來防止SQL注入。
#2
2
I am submitting the following, since you are not responding to comments I left under your question. If you left the question during the time of your posting, you can read the following:
我提交以下內容,因為您沒有回復我在您的問題下留下的評論。如果您在發布期間離開了問題,可以閱讀以下內容:
Seeing the title "How to insert into a table", it appears that you want to INSERT a new record.
看到標題“如何插入表格”,您似乎想要插入新記錄。
Your "Client ajouté avec succès" which is French for "The client has been successfully added", which also suggests you want to insert a new record.
您的“客戶端ajoutéavecsuccès”是法語“已成功添加客戶端”,這也表示您想要插入新記錄。
Edit: The above (French) was as per the original post before an update was made to the question.
編輯:上述(法語)是在對問題進行更新之前的原始帖子。
INSERT does not have a WHERE clause and that part of your code needs to be removed.
INSERT沒有WHERE子句,需要刪除部分代碼。
However, there is the INSERT ... ON DUPLICATE KEY UPDATE if you wish to continue using INSERT.
但是,如果您希望繼續使用INSERT,則會有INSERT ... ON DUPLICATE KEY UPDATE。
If the goal is to update an existing record, then you need to use "UPDATE":
如果目標是更新現有記錄,則需要使用“UPDATE”:
https://dev.mysql.com/doc/refman/5.7/en/update.html
Check for errors against the query using mysqli_error($connect):
使用mysqli_error($ connect)檢查查詢的錯誤:
http://php.net/manual/en/mysqli.error.php