Hack Forums
[PHP] Echo in while loop problem - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Echo in while loop problem (/showthread.php?tid=5942164)



Echo in while loop problem - blackcoder00 - Feb 17, 2019

hi guys i need help in this script the problem is :
in the first statment the script insert the values without error but after inserting he don't print the new values what are the mistakes i made guys ?

PHP Code

<?php 
$n1
=$_POST['fname'];$n2=$_POST['lname'];$n3=$_POST['cd'];


$khalil1=mysql_query("select * from client where nom='$n1'");
if(
mysql_num_rows($khalil1)==0){
 
$khalil2=mysql_query("insert into client values ('$n1','$n2','')");
 if(
$khalil2){
while(
$khalil10=mysql_fetch_array($khalil1)){

echo
"<br /> <b><p>Vente Profilée & Accessoire <div class='right'><p>Code client: ".$khalil10['code']." <br /> Nom Client : ".$n1." </br> MAT. fiscal :".$n2." <br />  <br /></p></div><br /><br /> Aluminium Center   <br /> <br />Rue Halab <br /> <br />Tel :  </p></b>";
}
}
}else if(
mysql_num_rows($khalil1)!=0){


while(
$khalil3=mysql_fetch_array($khalil1)){

echo
"<br /> <b><p>Vente Profilée & Accessoire <div class='right'><p>Code client: ".$khalil3['code']." <br /> Nom Client : ".$n1." </br> MAT. fiscal :".$n2." <br />  <br /></p></div><br /><br /> Aluminium Center  <br /> <br /> Rue Halab <br /> <br />Tel :  </p></b>";


}

} 
i think the problem is here guys :
PHP Code

<?php 
$khalil1
=mysql_query("select * from client where nom='$n1'");
if(
mysql_num_rows($khalil1)==0){
 
$khalil2=mysql_query("insert into client values ('$n1','$n2','')");
 if(
$khalil2){
while(
$khalil10=mysql_fetch_array($khalil1)){

echo
"<br /> <b><p>Vente Profilée & Accessoire <div class='right'><p>Code client: ".$khalil10['code']." <br /> Nom Client : ".$n1." </br> MAT. fiscal :".$n2." <br />  <br /></p></div><br /><br /> Aluminium Center   <br /> <br />Rue Halab <br /> <br />Tel :  </p></b>";
}
}
} 



RE: Echo in while loop problem - Abruptum - Feb 17, 2019

(Feb 17, 2019 03:25 PM)blackcoder00 Wrote:  hi guys i need help in this script the problem is :
in the first statment the script insert the values without error but after inserting he don't print the new values what are the mistakes i made guys ?

PHP Code

<?php 
$n1
=$_POST['fname'];$n2=$_POST['lname'];$n3=$_POST['cd'];


$khalil1=mysql_query("select * from client where nom='$n1'");
if(
mysql_num_rows($khalil1)==0){
 
$khalil2=mysql_query("insert into client values ('$n1','$n2','')");
 if(
$khalil2){
while(
$khalil10=mysql_fetch_array($khalil1)){

echo
"<br /> <b><p>Vente Profilée & Accessoire <div class='right'><p>Code client: ".$khalil10['code']." <br /> Nom Client : ".$n1." </br> MAT. fiscal :".$n2." <br />  <br /></p></div><br /><br /> Aluminium Center   <br /> <br />Rue Halab <br /> <br />Tel :  </p></b>";
}
}
}else if(
mysql_num_rows($khalil1)!=0){


while(
$khalil3=mysql_fetch_array($khalil1)){

echo
"<br /> <b><p>Vente Profilée & Accessoire <div class='right'><p>Code client: ".$khalil3['code']." <br /> Nom Client : ".$n1." </br> MAT. fiscal :".$n2." <br />  <br /></p></div><br /><br /> Aluminium Center  <br /> <br /> Rue Halab <br /> <br />Tel :  </p></b>";


}

} 
i think the problem is here guys :
PHP Code

<?php 
$khalil1
=mysql_query("select * from client where nom='$n1'");
if(
mysql_num_rows($khalil1)==0){
 
$khalil2=mysql_query("insert into client values ('$n1','$n2','')");
 if(
$khalil2){
while(
$khalil10=mysql_fetch_array($khalil1)){

echo
"<br /> <b><p>Vente Profilée & Accessoire <div class='right'><p>Code client: ".$khalil10['code']." <br /> Nom Client : ".$n1." </br> MAT. fiscal :".$n2." <br />  <br /></p></div><br /><br /> Aluminium Center   <br /> <br />Rue Halab <br /> <br />Tel :  </p></b>";
}
}
} 

Pleas for gods sake use mysqli+prepared statments your code quality is terrible...
Your problem lies in the logic of the inserting.
Steps you do:
1. request all clients with firstname x
2. check if there is one
3. you detect that its a new user
4. you insert a new user to the table
5. You then check for the result if teh adding was successful.
6. Now you go through the loop of gathered old users but you allready detected in the step 2-3 that there is no user ....

So you need to reload the users from teh database or do the work with your data you have loaded.

This is a Better Solution:
PHP Code

<?php 
$mysqli 
= mysqli_connect('localhost', 'root', '', 'test');
if (
mysqli_connect_errno()) {
    
printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$firstname = $_POST['ftname'];
$lastname = $_POST['ltname'];
$code = $_POST['cd'];
if (
$stmt = $mysqli->prepare("SELECT * FROM client WHERE fname = ?")) {
    
$stmt->bind_param("s", $firstname);
    
$result = $stmt->execute();
    if(
mysqli_num_rows($result) === 0){
        if (
$stmt = $mysqli->prepare("INSERT INTO client VALUES (?,?,'')")) {
            
$stmt->bind_param("ss", $firstname, $lastname);
            if(
$stmt->execute()){
                echo 
"<br/><b><p>Vente Profilée & Accessoire <div class='right'><p>Code client: {$code}<br/> Nom Client : {$firstname}</br> MAT. fiscal :{$lastname}<br/><br/></p></div><br/><br/> Aluminium Center<br/><br/>Rue Halab<br/><br/>Tel : </p></b>";
            }
        }
    }else{
        while(
$row = $result->fetch_assoc()){
            echo 
"<br/><b><p>Vente Profilée & Accessoire <div class='right'><p>Code client: {$row['code']}<br/> Nom Client : {$row['fname']}</br> MAT. fiscal :{$row['lname']}<br/><br/></p></div><br/><br/> Aluminium Center<br/><br/>Rue Halab<br/><br/>Tel : </p></b>";
        }
    }
} 
(not tested and renamed some of your database values so the code makes more sense now)