0

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!

register.html

<div class="padding" ng-controller="RegisterCtrl" ng-init="loadClient1()">

<a class="button button-info"  href="#/ajoutClient" style="background-color:#6EB4DC;float:right">Ajouter un client</a>
<table>
<tr>
          <th>Code.Client</th>
          <th>Nom</th>
          <th>Prenom</th>
          <td>Ajouter</td>
</tr>

<tr ng-repeat="x in names">
          <td ng-model="code">{{x.CodeClient}} </a> </td>
          <td ng-model="nom">{{x.NomClient}}       </td>
          <td ng-model="prenom">{{x.PrenomClient}}</td> >
          <td><a class="button button-info" ng-click="insertClient()" >Ajouter</a> </td>        
</tr>
</table>

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

 <?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';  
      }  
 }  
 ?>
5
  • use prepared staements not mysqli_real_escape_string to prevent SQL injection Commented Jun 2, 2017 at 10:34
  • do you want to INSERT a new record or UPDATE an existing one? Commented Jun 2, 2017 at 11:18
  • You can also INSERT ... ON DUPLICATE KEY UPDATE if that's what you want to do. Commented Jun 2, 2017 at 11:20
  • Voted to close as unclear. Did you leave the question? Edit (ping), either that or @Jens 's answer did not solve the question. You will need to place a comment under the answer, and/or "ping" one of us here. I for one can't look at this question for any much longer waiting for an update/response to my comments. Commented Jun 2, 2017 at 11:33
  • Note: I retracted my vote to close (as unclear), seeing the OP has clarified what they wish to do. Commented Jun 2, 2017 at 12:04

2 Answers 2

3

There is no INSERT INTO ... WHERE syntax. What you want to do is 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Right. However, the OP states: "it generates this error: undefinded property "code"" which is unrelated. Seems to be related to angular.
and their title reads as "How to insert into a table" - which to me, means just that; they seem to want to insert and not update. I placed a few comments under their question but they seem to not be present. And their "Client ajouté avec succès" is French for "The client has been successfully added", not "updated".
0

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.

  • 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.

However, there is the INSERT ... ON DUPLICATE KEY UPDATE if you wish to continue using INSERT.

If the goal is to update an existing record, then you need to use "UPDATE":

Check for errors against the query using mysqli_error($connect):

3 Comments

I m sorry to be late, and thank you for your answer, I will update my question as you suggest, but what I need is really an insert and not a update. because I want to insert data selected from a table 'client into another table 'clientattente' and not an update. Thank you
@SalamSalam you're welcome. Remove the WHERE CodeClient='$CodeClient' as I mentioned in my answer and along with everything else I wrote.
@SalamSalam however, if this is an angular-related issue, I won't be able to help you with that, since I do not have very much knowledge on that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.