Mysql database inserting two rows instead one

As per the query there should be only one row inserted but it inserts two rows with one row having null values except a few fields. The same query works fine on my local machine which means only one row gets inserted. What might be the problem?

can you share codes PLEASE?

I’m wondering do you double click on the link or the button that runs sql codes?

If you share your codes. we may be able to help you!

3 Likes

This is the form which has to be filled by the user:

<?php
include "config.php";

// Check user login or not
if(!isset($_SESSION['uname'])){
    header('Location: index.php');
}

// logout
if(isset($_POST['but_logout'])){
    session_destroy();
    header('Location: index.php');
}

    $welcome_query = mysqli_query($con,"select fullname, school_name from user where username='".$_SESSION['uname']."'");
    $welcomeQuery_result=mysqli_fetch_assoc($welcome_query);
    $_SESSION['school'] = $welcomeQuery_result['school_name']

?>
<!doctype html>
<html>
    <head>
    <link href="style.css" rel="stylesheet" type="text/css">
   <meta name="viewport" content="width=device-width,initial-scale=1">
    </head>
    <body>
        <div class="container">
        <h2> Welcome 
        <?php echo $welcomeQuery_result['fullname'];?>
        <br>
        School Name: <?php echo $_SESSION['school'];?> 
        </h2>
        
        <h4>Please fill and Submit this Internship Details Form </h4>
        
        
        <form method='post' action="internshipupdate.php">
            
            
            <div>
            Date of School Visit:<br>
            <input type="date" name="visitdate" required />
            </div>
            <div>
            In Time: <i> HH:MM AM/PM</i><br>
            <input type="time" name="in_time" required />
            </div>
            <div>
            Out Time: <i> HH:MM AM/PM</i><br>
            <input type="time" name="out_time" required />
            </div>
            <div>
  				Task 1 <br>
  				<textarea name="task1" rows="5" cols="30" required>
            </textarea>
            </div>
            <div>
  				Enter Task 2 (Optional) <br>
  				<textarea name="task2" rows="5" cols="30">
            </textarea>
            </div>
            <div>
  				Enter Task 3 (Optional) <br>
  				<textarea name="task3" rows="5" cols="30">
            </textarea>
            <br>
            </div>
            
            <input type="submit" value="Submit">
				</form>
            <br>
            <form method='post' action="">
				<input type="submit" value="Logout" name="but_logout" >
             
            </div>           
            
            
      </body>
</html>

This invoked internshipupdate.php whose code is given below:

<?php
include "config.php";

if(isset($_POST['but_logout'])){
    session_destroy();
    header('Location: index.php');
}


    $date = mysqli_real_escape_string($con,$_POST['visitdate']);
    $in_time = mysqli_real_escape_string($con,$_POST['in_time']);
    $out_time = mysqli_real_escape_string($con,$_POST['out_time']);
    //$school = mysqli_real_escape_string($con,$_POST['school']);
    $task1 = mysqli_real_escape_string($con,$_POST['task1']);
    $task2 = mysqli_real_escape_string($con,$_POST['task2']);
    $task3 = mysqli_real_escape_string($con,$_POST['task3']);
    $school = $_SESSION['school'];
    
    //query admission_id from user table
    $adm_query = mysqli_query($con,"select admission_id from user where username='".$_SESSION['uname']."'");
    $adm_result=mysqli_fetch_assoc($adm_query);
    $adm_id = $adm_result["admission_id"];
    
    //query mentor_id and school_id from mentor table
    $mentor_query = mysqli_query($con,"select mentor_admission_id, school_id from mentor where school_name='".$school."'");
    $mentor_result=mysqli_fetch_assoc($mentor_query);
    $mentor_admission_id = $mentor_result["mentor_admission_id"];
    $school_id=$mentor_result["school_id"];
    
    
    
    //insert internship details
    $sql = "INSERT INTO internshipdetail (admission_id, in_date, in_time, out_time, school_name, task1, task2, task3, mentor_admission_id, school_id) 
    VALUES ('$adm_id', '$date', '$in_time', '$out_time', '$school', '$task1', '$task2', '$task3', '$mentor_admission_id', '$school_id')";
		if($con->query($sql) == true){
    	echo "Your Internship details has been updated successfully.";
		} else{
    	echo "ERROR: Could not able to execute $sql. " . $con->error;
		}
		
		
 ?>
 
 <html>
 <head>
 <link href="style.css" rel="stylesheet" type="text/css">
 <meta name="viewport" content="width=device-width,initial-scale=1">
 </head>
            
            <div id="outer">
            <div class="inner">
            <form method='post' action="intern.php">   
            <input type="submit" value="Add More">
         	</form>
            </div>
            <div class="inner">
            <form method='post' action="">   
            <input type="submit" value="Logout" name="but_logout">
         	</form>
         	</div>
         	</div>
    
</html>

Did you try printing the $sql query before or after executing it to see it’s really the exact same data you’re entering? My first guess would be that some of the select statements you’re executing beforehand don’t return the right data.

1 Like

this works perfectly on my localhost. Duplicate row with some null value and some default value is created on the infinity free server. I am thinking on the lines of some setting problem like phpmyadmin on it’s UI always updates two rows unless u click on ignore.

i printed $sql and it shows the correct value as given below:

Your Internship details has been updated successfully.INSERT INTO internshipdetail (admission_id, in_date, in_time, out_time, school_name, task1, task2, task3, mentor_admission_id, school_id) VALUES ('t19001', '2019-05-03', '02:00', '06:00', 'Azim Premji School', 'task done 9 aug ', ' ', ' ', 't19002', '3')

but the table again shows one duplicate row with date, in_time, out_time as 0 and task as blank :frowning:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.