Click here to Skip to main content
14,831,225 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I dont know how to change the code to define the variable in php
i am trying to do form html to insert data into mysql database.

What I have tried:

<?php
$server = "localhost";
$username ="root";
$password = "";
$dbname = "booking";

$conn = mysqli_connect($server, $username, $password, $dbname);

if(isset($_POST['submit'])){
	 if(!empty($_POST['usertype']) && !empty($_POST['userid']) && !empty($_POST['usertype']) && !empty($_POST['bookingdate']) && !empty($_POST['startingtime']) && !empty($_POST['facility']) && !empty($_POST['payment']) )
$usertype=$_POST['usertype'];
$userid=$_POST['userid'];
$bookingdate=$_POST['bookingdate'];
$startingtime=$_POST['startingtime'];
$facility=$_POST['facility'];
$payment=$_POST['payment'];
}
$sql = "INSERT INTO booking(B_UserType,B_UserID, B_Date,B_StartTime,B_Facility ,B_PaymentType) VALUES ('$usertype','$userid','$bookingdate','$startingtime','$facility','$payment')";
  $run = mysqli_query($conn, $sql ) or die (mysqli_error());
if($run)
{
	echo "Inserted";
}else
{
	echo "Not inserted";
}
?>
Posted
Updated 20-Jul-20 23:09pm
Comments
Andre Oosthuizen 21-Jul-20 4:44am
   
Which variable is not defined? Show us your html code to see what you are posting.
steph99999 21-Jul-20 4:47am
   


<title>Insert Data






>
>
>
>
User Type User ID Booking Date Starting TimeFacility
Swimming attire rental
Changing area and showers
Personal locker
Sauna and steam room


Payment Type
Please Select
Cash
Debit
Credit Card










>
>
>
>
User Type User ID Booking Date Starting TimeFacility
Swimming attire rental
Changing area and showers
Personal locker
Sauna and steam room


Payment Type
Please Select
Cash
Debit
Credit Card
steph99999 21-Jul-20 4:48am
   
here's the html code



<title>Insert Data






>
>
>
>
User Type User ID Booking Date Starting TimeFacility
Swimming attire rental
Changing area and showers
Personal locker
Sauna and steam room


Payment Type
Please Select
Cash
Debit
Credit Card



Richard MacCutchan 21-Jul-20 4:54am
   
You have still not explained which variable is not defined.
steph99999 21-Jul-20 4:56am
   
all variable
Richard MacCutchan 21-Jul-20 5:17am
   
Please do not paraphrase. Show us the actual error messages that you see.
Richard Deeming 21-Jul-20 12:35pm
   
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]

You are missing the open and close brace for the if statement. But more importantly you are creating local variables in the if(isset, which are not visible to the following code. So it should be:
PHP
if(isset($_POST['submit'])){
    if(!empty($_POST['usertype']) && !empty($_POST['userid']) && !empty($_POST['usertype']) && !empty($_POST['bookingdate']) && !empty($_POST['startingtime']) && !empty($_POST['facility']) && !empty($_POST['payment']) ) { // ***** open brace needed here 
        $usertype=$_POST['usertype'];
        $userid=$_POST['userid'];
        $bookingdate=$_POST['bookingdate'];
        $startingtime=$_POST['startingtime'];
        $facility=$_POST['facility'];
        $payment=$_POST['payment'];
    
        // NB these lines need to be inside the two if blocks
        $sql = "INSERT INTO booking(B_UserType,B_UserID, B_Date,B_StartTime,B_Facility ,B_PaymentType) VALUES ('$usertype','$userid','$bookingdate','$startingtime','$facility','$payment')";
        $run = mysqli_query($conn, $sql ) or die (mysqli_error());
        if($run)
        {
            echo "Inserted";
        }else
        {
            echo "Not inserted";
        }
    } // ***** close brace needed here 
} // end of 'isset' test
   
v2
HTML



<!DOCTYPE html>
<html>    
    <head>    
        <title>Insert Data</title>    
    </head>   

    <body>    
	<form name= "details" action = "details.php" method ="post">
        <table>

<tr>
<td>User Type </td>
<td><input type = "text" name = "usertype" value =""></td>>
</tr>

<tr>
<td>User ID </td>
<td><input type = "text" name = "userid" value =""></td>>
</tr>

<tr>
<td>Booking Date </td>
<td><input type = "text" name = "bookingdate" value =""></td>>
</tr>

<tr>
<td>Starting Time</td>
<td><input type = "text" name = "startingtime" value =""></td>>
</tr>

<tr>
<td>Facility</td>
<td><select multiple size="4" name ="facility">
          <option value="attire">Swimming attire rental</option>
          <option value="changing">Changing area and showers</option>
          <option value="locker">Personal locker</option>
          <option value="steam">Sauna and steam room</option>
          
</select>
</td>
</tr>

<tr>
<td>Payment Type</td>
<td><select name ="payment">
<option value = "">Please Select</option>
<option value = "cash">Cash</option>
<option value = "debit">Debit</option>
<option value = "credit">Credit Card</option>
</select>
</td>
<tr>
<td><input type = "submit" value="Submit"></td>
</tr>

</table>
</form>
</html>    
   
Comments
Andre Oosthuizen 21-Jul-20 4:57am
   
ok, which variable is not defined?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900