Rewriting PHP script from mysqli to pdo code - php

I am rewriting my php code form mysqli to PDO, now I get a few error messages when I want to post my data.
I hope someone can help me im a starter with learning pdo :)
Over here a screenshot with the error messages
screenshot: https://gyazo.com/f4c6f6686014facb59d0fd40f1ebe522
<body>
<font color="black">
<form method="post" enctype='multipart/form-data'>
<table class="table-fill">
<thead>
<tr>
<th class="text-left">Film:</th>
<th class="text-left">
<input style="text" name="txtFilm">
</th>
</tr>
</thead>
<tbody class="table-hover">
<tr>
<td class="text-left">Genre:</td>
<td class="text-left">
<select name="Genre">
<option value="Actie">Actie</option>
<option value="Fantasie">Fantasie</option>
<option value="Horror">Horror</option>
<option value="Avontuur">Avontuur</option>
<option value="Komedie">Komedie</option>
<option value="Romantiek">Romantiek</option>
<option value="Historisch">Historisch</option>
</select>
</td>
</tr>
<tr>
<td class="text-left">Film omschrijving:</td>
<td class="text-left">
<textarea style="width:100%;resize:none;height:200px;" type="text" name="txtFilmomschrijving"></textarea>
</td>
</tr>
<tr>
<td class="text-left">Datum van uitkomst:</td>
<td class="text-left">
<input type="date" name="uitkomstdate">
</td>
</tr>
<tr>
<td class="text-left">Datum uit bioscoop:</td>
<td class="text-left">
<input type="date" name="Biosdate">
</td>
</tr>
<tr>
<td class="text-left">Film draai dagen:</td>
<td class="text-left">
<input type="checkbox" value="Maandag" name="draaidag[]">Maandag
<input type="checkbox" value="Dinsdag" name="draaidag[]">Dinsdag
<input type="checkbox" value="Woensdag" name="draaidag[]">Woensdag
<input type="checkbox" value="Donderdag" name="draaidag[]">Donderdag
<input type="checkbox" value="Vrijdag" name="draaidag[]">Vrijdag
<input type="checkbox" value="Zaterdag" name="draaidag[]">Zaterdag
<input type="checkbox" value="Zondag" name="draaidag[]">Zondag
</td>
</tr>
<tr>
<td class="text-left">Film tijd dagen:</td>
<td class="text-left">
<input type="checkbox" value="12uur" name="tijddagen[]">12:00
<input type="checkbox" value="14uur" name="tijddagen[]">14:00
<input type="checkbox" value="16uur" name="tijddagen[]">16:00
<input type="checkbox" value="18uur" name="tijddagen[]">18:00
<input type="checkbox" value="20uur" name="tijddagen[]">20:00
<input type="checkbox" value="22uur" name="tijddagen[]">22:00
<input type="checkbox" value="24uur" name="tijddagen[]">24:00
</td>
</tr>
<tr>
<td class="text-left">Zaal:</td>
<td class="text-left">
<select name="zaal">
<option value="normaal">Normaal</option>
<option value="groot">Groot</option>
</select>
</td>
</tr>
<tr>
<td class="text-left">Film afbeelding:</td>
<td class="text-left">
<input type="file" name="filmafbeelding" id="filmafbeelding">
</td>
</tr>
<tr>
<td class="text-left"></td>
<td class="text-left">
<center><input type="submit" value="Film toevoegen" name="btnHuur"></center>
</td>
</tr>
</tbody>
</table>
<br>
</form>
<br>
<?php
include ("loginDB.php");
if (isset($_POST['btnHuur'])) {
$filmimage =$_FILES["filmafbeelding"]["name"];
$filmimagemap = "images/";
$filmimagedoel = $filmimagemap.$filmimage;
$draaidagen = $_POST['draaidag'];
$draaidag="";
foreach($draaidagen as $draaidagcheck)
{
$draaidag.= $draaidagcheck.",";
}
$tijddagen = $_POST['tijddagen'];
$tijddag="";
foreach($tijddagen as $tijddagcheck)
{
$tijddag.= $tijddagcheck.",";
}
//film moet in de map /bioscoop/banner/ of /bioscoop/images/ staan//
$uploadfilm = "INSERT INTO films (film, movieimage, genre, Filmomschrijving, Datumvanuitkomst, Datumuitbioscoop, Filmdraaidagen, Filmtijddragen, Zaal) VALUES ('".$_POST["txtFilm"]."','$filmimagedoel','".$_POST["Genre"]."','".$_POST["txtFilmomschrijving"]."','".$_POST["uitkomstdate"]."','".$_POST["BiosDate"]."','$draaidag','$tijddag','".$_POST["zaal"]."')";
$result = mysqli_query($db,$uploadfilm);
echo "Film toegevoegt!";
}
?>
NOTE THIS IS MY OLD MYSQLI PHP CODE SCRIPT:
<center>
<div class="gallery1">
<h3>Film toevoegen</h3>
</div>
<br>
<br>
<form method="post" enctype='multipart/form-data'>
<table class="table-fill">
<thead>
<tr>
<th class="text-left">Film:</th>
<th class="text-left"><input style="text" name="txtFilm"></th>
</tr>
</thead>
<tbody class="table-hover">
<tr>
<td class="text-left">Genre:</td>
<td class="text-left"> <select name="Genre">
<option value="Actie">Actie</option>
<option value="Fantasie">Fantasie</option>
<option value="Horror">Horror</option>
<option value="Avontuur">Avontuur</option>
<option value="Komedie">Komedie</option>
<option value="Romantiek">Romantiek</option>
<option value="Historisch">Historisch</option>
</select></td>
</tr>
<tr>
<td class="text-left">Film omschrijving:</td>
<td class="text-left"><textarea style="width:100%;resize:none;height:200px;" type="text" name="txtFilmomschrijving"></textarea></td>
</tr>
<tr>
<td class="text-left">Datum van uitkomst:</td>
<td class="text-left"><input type="date" name="uitkomstdate"></td>
</tr>
<tr>
<td class="text-left">Datum uit bioscoop:</td>
<td class="text-left"><input type="date" name="Biosdate"></td>
</tr>
<tr>
<td class="text-left">Film draai dagen:</td>
<td class="text-left"><input type="checkbox" value="Maandag" name="draaidag[]">Maandag
<input type="checkbox" value="Dinsdag" name="draaidag[]">Dinsdag
<input type="checkbox" value="Woensdag" name="draaidag[]">Woensdag
<input type="checkbox" value="Donderdag" name="draaidag[]">Donderdag
<input type="checkbox" value="Vrijdag" name="draaidag[]">Vrijdag
<input type="checkbox" value="Zaterdag" name="draaidag[]">Zaterdag
<input type="checkbox" value="Zondag" name="draaidag[]">Zondag</td>
</tr>
<tr>
<td class="text-left">Film tijd dagen:</td>
<td class="text-left">
<input type="checkbox" value="12uur" name="tijddagen[]">12:00
<input type="checkbox" value="14uur" name="tijddagen[]">14:00
<input type="checkbox" value="16uur" name="tijddagen[]">16:00
<input type="checkbox" value="18uur" name="tijddagen[]">18:00
<input type="checkbox" value="20uur" name="tijddagen[]">20:00
<input type="checkbox" value="22uur" name="tijddagen[]">22:00
<input type="checkbox" value="24uur" name="tijddagen[]">24:00
</td>
</tr>
<tr>
<td class="text-left">Zaal:</td>
<td class="text-left"><select name="zaal">
<option value="normaal">Normaal</option>
<option value="groot">Groot</option></td>
</tr>
<tr>
<td class="text-left">Film afbeelding:</td>
<td class="text-left"><input type="file" name="filmafbeelding" id="filmafbeelding"></td>
</tr>
<tr>
<td class="text-left"></td>
<td class="text-left">
<center><input type="submit" value="Film toevoegen" name="submit"></center>
</td>
</tr>
</tbody>
</table>
</form>
</center>
<?php
if(isset($_POST["submit"])){
$hostname='localhost';
$username='root';
$password='';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=fastmovierenessefrank",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO films (film, movieimage, genre, Filmomschrijving, Datumvanuitkomst, Datumuitbioscoop, Filmdraaidagen, Filmtijddragen, Zaal)
VALUES ('".$_POST["txtFilm"]."','$filmimagedoel','".$_POST["Genre"]."','".$_POST["txtFilmomschrijving"]."','".$_POST["uitkomstdate"]."','".$_POST["BiosDate"]."','$draaidag','$tijddag','".$_POST["zaal"]."')";
if ($dbh->query($sql)) {
echo "Film toegevoegt!";
}
else{
echo "Er is iets misgegaan...";
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>

Here are the issues:
$_POST["BiosDate"] != name="Biosdate"
No $draaidag =
No $filmimagedoel =, this item also isn't present in the PDO HTML form.
No $tijddag =.
You aren't assigning in the PDO code like you did in the mysqli code. In additional the real benefit to using these drivers is there ability to use prepared statements and parameterized queries. I think this would be closer to what you want. You'll need to figure out what the $filmimagedoel is suppose to be:
if(isset($_POST["submit"])){
$hostname='localhost';
$username='root';
$password='';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=fastmovierenessefrank",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO films (film, movieimage, genre, Filmomschrijving, Datumvanuitkomst, Datumuitbioscoop, Filmdraaidagen, Filmtijddragen, Zaal)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$params = array($_POST["txtFilm"], $filmimagedoel, $_POST["Genre"], $_POST["txtFilmomschrijving"], $_POST["uitkomstdate"], $_POST["Biosdate"], implode(',', $_POST['draaidag']), implode(',', $_POST['tijddag']), $_POST["zaal"]);
$sth = $dbh->prepare($sql);
if ($sth->execute($params)) {
echo "Film toegevoegt!";
} else{
echo "Er is iets misgegaan...";
}
$dbh = null;
} catch(PDOException $e) {
echo $e->getMessage();
}
}
Additionally, storing delimited data in a DB is bad practice. You should only have one data value in a column.

Related

PHP function not returning values for dynamic drop down

I am facing issue in my dynamic drop down function it not return values i am unable to find where i'm doing wrong.One more issue i am facing regarding this code is that it not open drop down according to menus i.e if i select USA as a country it not show related cities of USA it always show all cities of all countries please set my code i will be highly thankful to you.
function listcatagoery()
{
$query="select * from tblcountry";
$rs=mysqli_query($query);
$html ='<select name="listcatagoery" id="listcatagoery" onchange="submitme();">';
while($row = mysqli_fetch_array($rs))
{
if($cntid == $row['cnt_id'])
{
$html.='<option selected="selected"
value="'.$row['cnt_id'].'">'.$row['cnt_name'].'</option>';
}
else
{
$html.='<option
value="'.$row['cnt_id'].'">'.$row['cnt_name'].'</option>';
}
}
$html.='</select>';
return $html;
}
///////////////////// List sub-catagoery////////
function listsubcatagoery()
{
$query = "select * from tblcity";
$rs=mysqli_query($query);
$html='<select name="listsubcatagoery" id="listsubcatagoery">';
while($row = mysqli_fetch_array($rs))
{
$html.='<option value="'.$row['cit_id'].'">
'.$row['cit_name'].'</option>';
}
$html.='</select>';
return $html;
}
<div id="main">
<div id="right">
<h2>User Registration Here...</h2><br />
<form name="frmadd" id="frmadd" action="" method="post" enctype="multipart/form-data">
<table border="1" cellpadding="5" cellspacing="4" width="90%" align="center">
<tr>
<td>Full Name</td>
<td><input type=text name="textname" size="40" id="textname" required="required"></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="rdgender" value="male" size="10">Male
<input type="radio" name="rdgender" value="Female" size="10">Female</td>
</tr>
<tr>
<td>Email</td>
<td><input type=text name="textemail" size="40" id="textemail" required="required"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="textpass" size="40" id="textpass" required="required"><span class="first"></span></td>
</tr>
<tr>
<td>Country</td>
<td>
<?php echo $htmlcountry; ?>
</td>
</tr>
<tr>
<td>City</td>
<td>
<?php echo $htmlcity; ?>
</td>
</tr>
<tr>
<td>Telephone</td>
<td><input type="number" name="txttel" size="11" id="txttel" required="required"></td>
</tr>
<tr>
<td>Photo</td>
<td><input type="file" name="txtphoto" size="40" id="txtphoto"></td>
</tr>
<tr>
<td>Signin</td>
<td colspan="2" align="center"><input type="submit" name="btnadd" id="btnadd" value="SigIn Now" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<?php echo $msg; ?>
</td>
</tr>
</table>
</form><br />
</div>

how to change text box values from mysql on click of drop down menu

I am at the very basic level of coding and I need help regarding this. You can post other links if there are any, related to this thing.
I want to get values from database dynamically on selection of city drop down and set it in a text box.
Any help will be appreciated.!!
enter image description here
<tr>
<td>
<label>City</label>
</td>
<td>
<select id="state" name="city" class="form-control" >
<?php
$sql = "SELECT city FROM service_point GROUP BY city";
$sel = mysql_query($sql);
// output data of each row
while($row = mysql_fetch_array($sel)) {
$city=$row['city'];
?>
<option value="<?php echo $row['city'] ?>">
<?php echo $row['city']; ?></option>
<?php }?>
</select>
</td>
</select>
</tr>
<tr>
<td> </td>
</tr>
<?php
include('config.php');
$s_name="";
$address="";
$phone_no="";
$email="";
$cperson="";
$city="";
$id=$_POST['city'];
$sql = "SELECT * FROM service_point where city = $id'' ";
$query = mysql_query($sql);
echo $sql;
while($row = mysql_fetch_array($query)){
?>
<tr>
<td> Name Of Service Point</td>
<td><input type="text" name="name" value="<?php echo $row['s_name'];?>"/></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Address </td>
<td><textarea name="address" col="5" value="<?php echo $row['address'];?>"></textarea></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Phone No</td>
<td><input type="text" name="phoneno" value="<?php echo $row[' phone_no'];?>"/></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Email Id</td>
<td><input type="text" name="email" value="<?php echo $row['email'];?>"/></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Contact Person</td>
<td><input type="text" name="cperson" value="<?php echo $row['cperson'];?>"/></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td></td>
<td align="center"><input type="submit" name="submit" value="submit"/><td>
</tr>
</table>
<?php }?>
</form>

Multiple checkbox to a sentence before insert to mysql

I' edit this question with my full coding
Let me post my full code here.
Page1.php
<div id="container">
<div class="box">
<form name="form" method="post" action="userbooking2.php">
<table width="" style="border: 0px solid black" align="center">
<tr>
<td><h2>Book a Room Step 1 - Booking Information</h2></td>
</tr>
<tr>
<td>
<table>
<tr>
<td width="150px" class="titlestyle">Booked by</td>
<td width="400px"><span style="text-transform: uppercase;"><?php echo $_SESSION['SESS_FNAME']; ?></span></td>
</tr>
<tr>
<td class="titlestyle">Title</td>
<td><span class="hint--right" data-hint="Hint : Organization meeting"><input type="text" name="booking_title" id="booking_title" maxlength="50" size="50" /></span></td>
</tr>
<tr>
<td class="titlestyle">Date</td>
<td>From <input type="text" name="booking_start_date" id="booking_start_date"/> to <input type="text" name="booking_end_date" id="booking_end_date"/></td>
</tr>
<tr>
<td class="titlestyle">Time</td>
<td>From <input name="booking_start_time" id="booking_start_time" value="8:00 AM" /> to <input name="booking_end_time" id="booking_end_time" value="8:30 AM"/></td>
</tr>
<tr>
<td class="titlestyle">Room</td>
<td><select name="room_type" id="room_type">
<option value="none">--Select--</option>
<option value="Meeting Room">Meeting Room</option>
<option value="Lecture Room">Lecture Room</option>
<option value="Computer Lab">Computer Lab</option>
</select>
</td>
</tr>
<tr>
<td class="titlestyle">Participant</td>
<td><span class="hint--right" data-hint="Hint : Insert number of participants"><input type="text" name="room_participant" id="room_participant" maxlength="50" size="3" /></span> Persons</td>
</tr>
<tr>
<td class="titlestyle">Equipment</td>
<td><table>
<tr>
<td><input type="checkbox" name="room_facility[]" value="Audio System" />Audio System</td>
<td><input type="checkbox" name="room_facility[]" value="Projector" />Projector</td>
<td><input type="checkbox" name="room_facility[]" value="Video Conferencing" />Video Conferencing</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="titlestyle">Food & Baverages</td>
<td><table>
<tr>
<td><input type="checkbox" name="room_food[]" value="Breakfast" />Breakfast</td>
<td><input type="checkbox" name="room_food[]" value="Tea Break" />Tea Break</td>
<td><input type="checkbox" name="room_food[]" value="Lunch" />Lunch</td>
</tr>
<tr>
<td><input type="checkbox" name="room_food[]" value="High Tea" />High Tea</td>
<td><input type="checkbox" name="room_food[]" value="Dinner" />Dinner</td>
<td><input type="checkbox" name="room_food[]" value="No" />No, thanks</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="titlestyle">Additional Info</td>
<td><span class="hint--right" data-hint="Hint : ."><textarea name="booking_desc" id="booking_desc" style="max-width:400px;" rows="4" cols="50"></textarea></span></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="Submit" value="Next" /></td>
</tr>
</table>
</form>
</div>
Page2.php
<?php
//now, let's register our session variables
session_register('fname');
session_register('booking_title');
session_register('booking_start_date');
session_register('booking_end_date');
session_register('booking_start_time');
session_register('booking_end_time');
session_register('room_participant');
session_register('room_type');
session_register('booking_facility');
session_register('booking_food');
session_register('booking_desc');
//finally, let's store our posted values in the session variables
$_SESSION['fname'] = $_SESSION['SESS_FNAME'];
$_SESSION['booking_title'] = $_POST['booking_title'];
$_SESSION['booking_start_date'] = $_POST['booking_start_date'];
$_SESSION['booking_end_date'] = $_POST['booking_end_date'];
$_SESSION['booking_start_time'] = $_POST['booking_start_time'];
$_SESSION['booking_end_time'] = $_POST['booking_end_time'];
$_SESSION['room_participant'] = $_POST['room_participant'];
$_SESSION['room_type'] = $_POST['room_type'];
$_SESSION['room_facility'] = /* WHAT TO SEND HERE? */
$_SESSION['room_food'] = $_POST['room_food'];
$_SESSION['booking_desc'] = $_POST['booking_desc'];?>
// Other form process here
Page3.php
<div id="container">
<div class="box">
<form name="form" method="post" action="">
<table width="" style="border: 0px solid black" align="center">
<tr>
<td><h2>Book a Room Step 3 - Final</h2></td>
</tr>
<tr>
<td>
<table>
<tr>
<td width="150px" class="titlestyle">Booked by</td>
<td width="400px" ><span style="text-transform: uppercase;"><?php echo $_SESSION['SESS_FNAME']; ?></span></td>
</tr>
<tr>
<td class="titlestyle">Title</td>
<td><?php echo $_SESSION['booking_title']; ?></td>
</tr>
<tr>
<td class="titlestyle">Date</td>
<td>From <?php echo $_SESSION['booking_start_date']; ?> to <?php echo $_SESSION['booking_end_date']; ?></td>
</tr>
<tr>
<td class="titlestyle">Time</td>
<td>From <?php echo $_SESSION['booking_start_time']; ?> to <?php echo $_SESSION['booking_end_time']; ?></td>
</tr>
<tr>
<td class="titlestyle">Room</td>
<td><?php echo $_SESSION['room_name']; ?></td>
</tr>
<tr>
<td class="titlestyle">Participant</td>
<td><?php echo $_SESSION['room_participant']; ?> Persons</td>
</tr>
<tr>
<td class="titlestyle">Equipment</td>
<td><?php echo $_SESSION['room_facility']; ?></td>
</tr>
<tr>
<td class="titlestyle">Food & Baverages</td>
<td><?php echo $_SESSION['room_food']; ?></td>
</tr>
<tr>
<td class="titlestyle">Additional Info</td>
<td><?php echo $_SESSION['booking_desc']; ?></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>
</div>
In page3.php, i want to view this kind of string "I book Value1, Value2" before store it into the database
Use:
$string = implode(', ', isset($_REQUEST['room_facility']) ? $_REQUEST['room_facility'] : array());
echo 'I need ' . $string;
Then just insert into the database as normal

php - drop down from database

Im a newbie ih php.Im trying to show drop down list values from database using this but the data not shown up.There is no error as well. Need some experts advice.
Code
<?php
include('connectdb.php');
$sql="SELECT id,name FROM companydetailstbl";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
//echo $id;
$thing=$row["name"];
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
<table width="900" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%">Company Name </td>
<td width="3%">:</td>
<td width="75%"><SELECT NAME="thing">
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT></td>
</tr>
<tr>
<td>Installation Date </td>
<td>:</td>
<td><input type="text" name="ins_dt" />
(ddmmyyyy)</td>
</tr>
<tr>
<td>Expiry Date </td>
<td>:</td>
<td><input type="text" name="ex_dt" />
(ddmmyyyy)</td>
</tr>
<tr>
<td>Status</td>
<td>:</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Update" />
<input type="submit" name="Submit2" value="Reset" /></td>
</tr>
</table>
Please help. Thanks
I don't know what DTD you are currently using, but according to the HTML 4.0 Specification, you need to close your option tag.
$options .= '<option value="' . $id . '">' . $thing . '</option>';
Also there,
<select name="thing">
<option value="0">Choose</option>
<?php echo $options; ?>
</select>
please try to close the tag
$options.="<OPTION VALUE=\"$id\">".$thing."</OPTION>";
also edit
<SELECT NAME="thing">
<OPTION VALUE=0>Choose</OPTION>
<?=$options?>
</SELECT>
<?php
include('connectdb.php');
$sql="SELECT id,name FROM companydetailstbl";
$result=mysql_query($sql);
?>
<table width="900" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%">Company Name </td>
<td width="3%">:</td>
<td width="75%">
<select name="thing">
<?php
while($row = mysql_fetch_object($result)){
?>
<option value="<?=$row["id"]; ?>"><?=$row["name"]; ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Installation Date </td>
<td>:</td>
<td><input type="text" name="ins_dt" />(ddmmyyyy)</td>
</tr>
<tr>
<td>Expiry Date </td>
<td>:</td>
<td><input type="text" name="ex_dt" />(ddmmyyyy)</td>
</tr>
<tr>
<td>Status</td>
<td>:</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<input type="submit" name="Submit" value="Update" />
<input type="submit" name="Submit2" value="Reset" />
</td>
</tr>
</table>
Hope it helps

PHP inserting into database

Hey guys, im trying to insert some checkboxes into a database and im pretty sure my code is correct however i keep getting the error ERROR INSERTING: Column count doesn't match value count at row 1
Basically i am adding each checkbox to a different column in my database
Here is my code
$idextra=$_POST['extras'];
$arr_num=count($idextra);
$i=0;
while ($i < $arr_num)
{
$qu="INSERT INTO bs_reservations (deodoriser,carpet,carpetrepair,furniture,tabs,urine) VALUES ('$idextra[$i]')";
$res=mysql_query($qu) or die('ERROR INSERTING: '.mysql_error());
$i++;
}
Hey guys here is the HTML for my check boxes and contact form.
`
<tr>
<td height="30" align="right" class="align_right">Your Name*: </td>
<td>
<input type="text" name="name" id="name" value="<?php echo $name?>" onchange="checkFieldBack(this)"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Phone*: </td>
<td><input type="text" name="phone" id="phone" value="<?php echo $phone?>" onchange="checkFieldBack(this)" onkeyup="noAlpha(this)"/></td>
</tr>
<tr>
<td height="30" align="right" class="align_right">E-mail*: </td>
<td><input type="text" name="email" id="email" value="<?php echo $email?>" onchange="checkFieldBack(this);"/></td>
</tr>
<tr>
<td align="right" valign="top" class="align_right">Address*: </td>
<td><textarea name="comments" id="comments" cols="15" rows="5" onchange="checkFieldBack(this)"><?php echo $comments?></textarea></td>
</tr>
<tr>
<td width="236" height="25" align="left">Drop off at:</td>
<td width="548" height="23"><select name="dropoff">
<option value="05:00" <?php echo $dropoff=="05:00"?"selected":""?>>05:00</option>
<option value="06:00" <?php echo $dropoff=="06:00"?"selected":""?>>06:00</option>
<option value="07:00" <?php echo $dropoff=="07:00"?"selected":""?>>07:00</option>
<option value="08:00" <?php echo $dropoff=="08:00"?"selected":""?>>08:00</option>
<option value="09:00" <?php echo $dropoff=="09:00"?"selected":""?>>09:00</option>
<option value="10:00" <?php echo $dropoff=="10:00"?"selected":""?>>10:00</option>
<option value="11:00" <?php echo $dropoff=="11:00"?"selected":""?>>11:00</option>
<option value="12:00" <?php echo $dropoff=="12:00"?"selected":""?>>12:00</option>
<option value="13:00" <?php echo $dropoff=="13:00"?"selected":""?>>13:00</option>
<option value="14:00" <?php echo $dropoff=="14:00"?"selected":""?>>14:00</option>
<option value="15:00" <?php echo $dropoff=="15:00"?"selected":""?>>15:00</option>
<option value="16:00" <?php echo $dropoff=="16:00"?"selected":""?>>16:00</option>
<option value="17:00" <?php echo $dropoff=="17:00"?"selected":""?>>17:00</option>
<option value="18:00" <?php echo $dropoff=="18:00"?"selected":""?>>18:00</option>
<option value="19:00" <?php echo $dropoff=="19:00"?"selected":""?>>19:00</option>
</select>
</td>
<tr>
<td height="10" align="right" class="align_right">Deodoriser: </td>
<td>
<input type="checkbox" name="extras[]" id="deodoriser" value="Deodoriser>"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Carpet Protector (5 litre): </td>
<td>
<input type="checkbox" name="extras[]" id="carpet" value="Carpet Protector (5 litre)"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Carpet Repair Tools: </td>
<td>
<input type="checkbox" name="extras[]" id="carpetrepair" value="Carpet Repair Tools"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Furniture Moving Equipment: </td>
<td>
<input type="checkbox" name="extras[]" id="furniture" value="Furniture Moving Equipment"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Furniture Tabs: </td>
<td>
<input type="checkbox" name="extras[]" id="tabs" value="Furniture Tabs"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Urine Decontamination Treatment: </td>
<td>
<input type="checkbox" name="extras[]" id="urine" value="Urine Decontamination Treatment"/>
</td>
</tr>
`
and here is my complete php code for inserting into the data base
`$idextra=$_POST['extras'];
$arr_num=count($idextra);
$i=0;
while ($i < $arr_num)
{
$qu="INSERT INTO bs_reservations (deodoriser,carpet,carpetrepair,furniture,tabs,urine) VALUES ('{$idextra[1]}','{$idextra[2]}','{$idextra[3]}','{$idextra[4]}','{$idextra[5]}','{$idextra[6]}')";
$res=mysql_query($qu) or die('ERROR INSERTING: '.mysql_error());
$i++;
}
$q="INSERT INTO bs_reservations (dateCreated, name, email, phone, comments,status,eventID, qty,dropoff) VALUES (NOW(),'".$name."','".$email."','".$phone."','".$comments."','2','".$eventID."','".$qty."','".$dropoff."')";
$res=mysql_query($q) or die("error!");
$orderID=mysql_insert_id();`
I basically want to take all the inputs that the user selects and insert them into a data base.
You have these columns:
(deodoriser,carpet,carpetrepair,furniture,tabs,urine)
And you are inserting this:
'$idextra[$i]'
That's 6 columns and 1 value. As the error says, that's not the same.
You might have meant something like this:
('{$idextra[1]}','{$idextra[2]}','{$idextra[3]}','{$idextra[4]}','{$idextra[5]}','{$idextra[6]}')
If you want to make a string out of your array beforehand, use something like this using implode
$yourString = implode("','",$idextra);
$qu="INSERT INTO bs_reservations (deodoriser,carpet,carpetrepair,furniture,tabs,urine)
VALUES ('{$yourString}')";
echo the query to be sure it's sane :)
Instead of using array for your checkboxes, give them distinct names, e.g.
<input type="checkbox" name="carpetrepair" id="carpetrepair" value="Carpet Repair Tools"/>
And then check if any of them were checked:
$options = explode(",","deodoriser,carpet,carpetrepair,furniture,tabs,urine");
$sql = "INSERT INTO bs_reservations SET ";
foreach($options as $opt){
if (isset($_POST[$opt])) {
$sql.= "`$opt`=1,";
}
}
$sql = rtrim($sql,",");

Categories