1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Some data is not been inserted to database with php

Discussion in 'PHP & Perl' started by shizzledizzleeee, Oct 3, 2016.

  1. shizzledizzleeee

    shizzledizzleeee Registered Member

    Joined:
    Jan 3, 2013
    Messages:
    73
    Likes Received:
    6
    only some data is being inserted to database checkbox array is only storing one word to database instead of saving the whole string separated with (,) but when i echo the variable it displays the whole array on the bowser what could be the problem, also the image part is not saving the the image details on the database

    [​IMG]
    <?php
    $host = 'localhost';
    $username = 'root';
    $password = '';
    $datadase = 'registerfinal';
    $connect = mysqli_connect($host, $username, $password) or die ('error to connect to datadase'.mysqli_error());
    if ($connect) {
    echo 'mysqli connect succsessfull';
    }
    echo '<br /><br />';
    $selectdb = mysqli_select_db($connect, $datadase) or die ('unable to select datadase'.mysqli_error());
    if($selectdb) {
    echo 'database selected succsessfully';
    }

    if(isset($_POST['savedetails'])) {

    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $repeat_password = $_POST['repeat_password'];
    $gender = $_POST['gender'];
    $country = $_POST['country'];


    if(isset($_POST['food'])) {
    $food = $_POST['food'];
    $favfood = "";
    foreach($food as $meal ) {
    $favfood = $meal.",";
    print_r($favfood);
    }
    }

    if(isset($_POST['imageUpload'])) {
    $imageUploadname = $_FILES['imageUpload']['name'];
    $imageUploadsize = $_FILES['imageUpload']['size'];
    $imageUploadtmp_name = $_FILES['imageUpload']['tmp_name'];
    $imageUploadtype = $_FILES['imageUpload']['type'];
    $uploadFolder = "uploadFolder/";
    $destinationName = rand(1000, 10000).$imageUploadname;

    move_uploaded_file($imageUploadtmp_name, $uploadFolder.$destinationName);
    echo "$imageUploadname";
    echo "$imageUploadsize";
    echo "$imageUploadtmp_name";
    echo "$imageUploadtype";
    echo "$destinationName";

    }
    $sqltwo = "INSERT INTO `registerfinaltable` (`id`, `firstname`, `lastname`, `username`, `password`, `repeat_password`,
    `gender`, `food`, `country`, `imageUploadname`, `imageUploadsize`, `imageUploadtype`)
    VALUES (NULL, '$firstname', '$lastname', '$username', '$password', '$repeat_password', '$gender', '$favfood', '$country',
    '$destinationName', '$imageUploadsize', '$imageUploadtype')";

    $results = mysqli_query($connect, $sqltwo) ;
    if($results){

    echo "inserted successfully";
    }
    }

    ?>


    <html>
    <head>
    <title>register</title>

    </head>

    <body>
    <form action = "" method = "post" enctype = "multipart/form-data" >
    <label>first name : <input type = "text" name = "firstname" /> </label> <br /><br />
    <label>last name : <input type = "text" name = "lastname" /> </label><br /><br />
    <label>username : <input type = "text" name = "username" /> </label><br /><br />
    <label>password : <input type = "password" name = "password" /> </label><br /><br />
    <label>repeat password : <input type = "password" name = "repeat_password" /> </label><br /><br />
    <label>Male : <input type = "radio" name = "gender" value = "Male" /> </label><br /><br />
    <label>Female : <input type = "radio" name = "gender" value = "Female" /> </label><br /><br />
    <label>pizza : <input type = "checkbox" name = "food[]" value = "pizza"/> </label><br /><br />
    <label>burger : <input type = "checkbox" name = "food[]" value = "burger"/> </label><br /><br />
    <label>chips : <input type = "checkbox" name = "food[]" value = "chips"/> </label><br /><br />
    <label>sausage : <input type = "checkbox" name = "food[]" value = "sausage"/> </label><br /><br />
    <label>sandwich : <input type = "checkbox" name = "food[]" value = "sandwich"/> </label><br /><br />
    <label>Image : <input type = "file" name = "imageUpload" /> </label><br /><br />
    <select name = "country">
    <?php
    $sql = 'SELECT * FROM `countrie` ';
    $querry = mysqli_query($connect, $sql);
    while($country = mysqli_fetch_array($querry)):;

    ?>

    <option value = "<?php echo $country['country']; ?>"><?php echo $country['country']; ?></option>
    <?php endwhile;?>


    </select> <br />


    <input type = "submit" name = "savedetails" />



    </form>
    <table border = "1" bgcolor = "" width = "100%">
    <tr><th>id</th><th>Firstname</th><th>Lastname</th><th>Username</th><th>Password</th><th>Password 2</th><th>Gender</th><th>Fav. Food</th> <th>Image</th> <th>Country</th><th>imageUploadname</th><th>imageUploadsize</th><th>imageUploadtype</th></tr>
    <?php
    $sqldata = "SELECT * FROM registerfinaltable";
    $querysqldata = mysqli_query($connect, $sqldata);
    while($rows = mysqli_fetch_array($querysqldata) ):;
    ?>
    <tr>
    <td><?php echo $rows['id'];?></td>
    <td><?php echo $rows['firstname'];?></td>
    <td><?php echo $rows['lastname'];?></td>
    <td><?php echo $rows['username'];?></td>
    <td><?php echo $rows['password'];?></td>
    <td><?php echo $rows['repeat_password'];?></td>
    <td><?php echo $rows['lastname'];?></td>
    <td><?php echo $rows['gender'];?></td>
    <td><?php echo $rows['food'];?></td>
    <td><?php echo $rows['country'];?></td>
    <td><?php echo $rows['imageUploadname'];?></td>
    <td><?php echo $rows['imageUploadsize'];?></td>
    <td><?php echo $rows['imageUploadtype'];?></td>
    <?php endwhile;?>
    </tr>
    </table>
    </body>
    </html>​
     
  2. Repulsor

    Repulsor Power Member

    Joined:
    Jun 11, 2013
    Messages:
    770
    Likes Received:
    278
    Location:
    PHP Scripting ;)
    Before

    $results = mysqli_query($connect, $sqltwo) ;

    echo $sqltwo and paste the SQL query here so we could take a look at it with the sample data being inserted into the database.
     
  3. shizzledizzleeee

    shizzledizzleeee Registered Member

    Joined:
    Jan 3, 2013
    Messages:
    73
    Likes Received:
    6
    [​IMG]
     
  4. lovejesus

    lovejesus Regular Member

    Joined:
    Jul 28, 2013
    Messages:
    200
    Likes Received:
    25
    Occupation:
    Freelancer
    Location:
    UK
    Your code is not safe .
     
  5. shizzledizzleeee

    shizzledizzleeee Registered Member

    Joined:
    Jan 3, 2013
    Messages:
    73
    Likes Received:
    6
    yeah i know i want it to work first so that i can secure it
     
  6. Repulsor

    Repulsor Power Member

    Joined:
    Jun 11, 2013
    Messages:
    770
    Likes Received:
    278
    Location:
    PHP Scripting ;)
    Uploadsize and Uploadtype seems to be empty.

    var_dump($_FILES['imageUpload']) and see what all datas are present.
     
  7. shizzledizzleeee

    shizzledizzleeee Registered Member

    Joined:
    Jan 3, 2013
    Messages:
    73
    Likes Received:
    6
    [​IMG]
     
  8. shizzledizzleeee

    shizzledizzleeee Registered Member

    Joined:
    Jan 3, 2013
    Messages:
    73
    Likes Received:
    6
    [​IMG]
     
  9. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,927
    Likes Received:
    1,944
    Home Page:
    Your checkbox loop is overwriting itself:
    PHP:
    $favfood "";
    foreach(
    $food as $meal ) {
    $favfood $meal.",";
    print_r($favfood);
    }
    Should be:
    PHP:
    $favfood "";
    foreach(
    $food as $meal ) {
    $favfood $meal.",".$favfood;
    print_r($favfood);
    }
     
  10. xord

    xord Newbie

    Joined:
    Oct 23, 2016
    Messages:
    28
    Likes Received:
    2
    Gender:
    Male
    As mentioned above, you're overwriting the variable on each loop rather than appending to a string.

    Assuming you are trying to create a comma separated list of foods from the array as a single string...

    Change all of the below:
    PHP:
    if(isset($_POST['food'])) {
    $food $_POST['food'];
    $favfood "";
    foreach(
    $food as $meal ) {
    $favfood $meal.",";
    print_r($favfood);
    }
    }
    To
    PHP:
    $favfood = (isset($_POST['food'])) ? explode(', '$_POST['food']) : '';
     
    • Thanks Thanks x 1