angularjs怎么与php数据交互,如何使用AngularJS和php后端插入表中

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

Ajouter un client

Code.ClientNomPrenomAjouter
{{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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值