How to add more than one sql

How to add more than one sql
0.0 0

#1

Hey guys!

I am trying to insert another table and I thought that I had it figured out from my other registration page that I had done but for some reason, it is not inserting into my rewards table… Have I placed the code in the correct place? after the correct {}? I have set most of my variables to null in my database and there are 3 tinyints set to 0 but i should be able to insert these three variables…


<?php

if (!isset($_POST['submit'])) {
	header("Location: index.php?student_registration_form=error");
	exit();
} else {

	include_once 'includes/dbh.php';

	$first_name = $_POST['first_name'];
	$last_name = $_POST['last_name'];
	$age = $_POST['age'];
	$school_grade = $_POST['school_grade'];
	$music_experience = $_POST['music_experience'];
	$piano_grade = $_POST['piano_grade'];

	$sql = "SELECT first_name, last_name FROM student_details";

	$stmt = mysqli_stmt_init($conn);

    	if(!mysqli_stmt_prepare($stmt, $sql)) {
    		echo 'SQL error';
    	} else {
    		mysqli_stmt_bind_param($stmt, "ss", $first_name, $last_name);
    		mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
            $resultCheck = mysqli_num_rows($result);

            if ($resultCheck > 0) {
            	header("Location: student_registration.php?student_registration_form=exists");
            	exit();
            }else {
            	if ($first_name === '' || $last_name === '' || $age === '' || $music_experience === '' || $piano_grade === '') {
    	header("Location: student_registration.php?student_registration_form=nofilledout");
    	exit();
    } else {

    	$sql = "INSERT INTO student_details (first_name, last_name, age, school_grade, previous_experience, piano_grade) VALUES (?,?,?,?,?,?);";

    	$stmt = mysqli_stmt_init($conn);

    	if(!mysqli_stmt_prepare($stmt, $sql)) {
    		echo 'SQL error';
    	} else {
    		mysqli_stmt_bind_param($stmt, "ssiiss", $first_name, $last_name, $age, $school_grade, $music_experience, $piano_grade);
    		mysqli_stmt_execute($stmt);

    		
    	} 

          
         $sql = "INSERT INTO rewards (user_uid, first_name, last_name) VALUES (?,?,?)";

                              $stmt = mysqli_stmt_init($conn);
                              if (!mysqli_stmt_prepare($stmt, $sql)) {
                                 echo 'SQL error';
                              } else {
                                mysqli_stmt_bind_param($stmt, "sss", $uid, $first_name, $last_name);
                                mysqli_stmt_execute($stmt);
                              }

    	    


    	} 

        header("Location: index.php?registration=success");
            exit();

       


    	}


    	} 



	
 
    
            
    	
    



    }
	
	


#2

You are binding $first_name and $last_name parameters but WHERE clause is missing from query $sql = “SELECT first_name, last_name FROM student_details”;

Also the $uid variable is not declared anywhere but being used in binding mysqli_stmt_bind_param($stmt, “sss”, $uid, $first_name, $last_name);


#3

Sorry for the late reply… thanks!


#4

Your’e welcome! :slightly_smiling_face: