I am developing a script for a table called 'minivan'. Here is the table structure sql fiddle link. Admin provides seats for minivan.
I want to check if seats are available from this search form: search form link The method is GET.
First i want to check the date [this is format of date: 2015-02-16 for example] that a user submits. If the date matches i want to proceed with 'from' and 'to' location that a user chooses. If records are found i want to check what is the total passenger from the dropdown list for different ages that a users provides.
If the total number of passenger does not exceed the seat number for a particular day that a user chooses, i want to echo "seats are available". and proceed with other parts that i am planning to develop later.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_GET['submit'])) {
$date = $_GET['date_up'];
$query = "SELECT date_up FROM minivan WHERE date_up = '$date'";
$result = mysqli_query($conn, $query);
$numrows = mysqli_num_rows($result);
//print_r($numrows);
if ($numrows)
{
$startPlace = $_GET['startpoint'];
$query2 = "SELECT startpoint FROM minivan WHERE startpoint = '$startPlace'";
$result2 = mysqli_query($conn, $query2);
//print_r($result2);
$numrows2 = mysqli_num_rows($result2);
$endPlace = $_GET['endpoint'];
$query3 = "SELECT endpoint FROM minivan WHERE endpoint = '$endPlace'";
$result3 = mysqli_query($conn, $query3);
//print_r($result2);
$numrows3 = mysqli_num_rows($result3);
if ($numrows2 && $numrows3) {
$adult = $_GET['adult'];
$juvenile = $_GET['juvenile'];
$kids = $_GET['kids'];
$child = $_GET['child'];
$totalPassenger = $adult + $juvenile + $kids + $child ;
$seatQuery = "SELECT seat FROM minivan WHERE seat > $totalPassenger";
$result4 = mysqli_query($conn, $seatQuery);
$numrows4 = mysqli_num_rows($result4);//shows error here how to fix it?
if ($numrows4) {
while ($row = mysqli_fetch_row($result4)) {
$seats = $row[0];
echo "$seats are availble for the $date you selected";
}
} else {
echo "no seats are found";
}
}
} else {
"start point and end point does not match";
}
}
$conn->close();
?>
It shows error here $numrows4 = mysqli_num_rows($result4);//shows error here how to fix it?
here is the form i am trying to search with
<form action="" method="GET">
From
<select name="startpoint" id="from">
<option >Hat Yai Airport</option>
<option >Pak Bara</option>
<option >Kohlipe</option>
</select>
To
<select name="endpoint" id="to">
<option >Pak Bara</option>
<option >Hat Yai Airport</option>
<option >Kohlipe</option>
</select>
<label for="Date">Date</label>
<input type="text" name="date_up" id="datepicker">
<h4 style="margin-top: 30px;">Passengers</h4>
Adults
<select name="adult" id="from">
<option >1</option>
<option >2</option>
<option >3</option>
<option >4</option>
<option >5</option>
</select>
< 12 years
<select name="juvenile" id="to">
<option >0</option>
<option >1</option>
<option >2</option>
<option >3</option>
<option >4</option>
</select>
< 7 years
<select name="kids" id="from">
<option >0</option>
<option >1</option>
<option >2</option>
<option >3</option>
</select>
< 3 years
<select name="child" id="to">
<option >0</option>
<option >1</option>
<option >2</option>
<option >3</option>
</select>
<input type="submit" value="Search" class="submit" name="submit">
Can't really figure out whats wrong with this. I am beginner in php and mysql. Please help me solve this.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to take the input from drop down menus that users enter and submit them to a table in my database. I am trying to submit the values into this table:
I use the POST to check that the values are being pulled from the HTML form and they are, but they won't submit into my table. I've made sure that all of the names with the columns and HTML forms are correct, why won't the values post to the table?
<?php
$databaseName = 'pizza_db';
$databaseUser = 'root';
$databasePassword = 'root';
$databaseHost = '127.0.0.1';
$conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected sucessfully\n";
if(isset($_POST['submit'])){
$value = mysqli_real_escape_string($conn,$_POST['drink']);
$value2 = mysqli_real_escape_string($conn,$_POST['cheese']);
$value3 = mysqli_real_escape_string($conn,$_POST['veggies']);
$value4 = mysqli_real_escape_string($conn,$_POST['meat']);
$value5 = mysqli_real_escape_string($conn,$_POST['sauce']);
$value6 = mysqli_real_escape_string($conn,$_POST['crust']);
$value7 = mysqli_real_escape_string($conn,$_POST['size']);
$sql = "INSERT INTO order_info(drink,cheese,veggies,meat,sauce,crust,size)
VALUES('$value','$value2','$value3','$value4','$value5','$value6','$value7')";
//Here I am posting the values to check that they are being submitted
echo $_POST["size"];
echo "\n";
echo $_POST["sauce"];
echo "\n";
echo $_POST["crust"];
echo "\n";
echo $_POST["cheese"];
echo "\n";
echo $_POST["meat"];
echo "\n";
echo $_POST["veggies"];
echo "\n";
echo $_POST["drink"];
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<body>
<form action='' method='post'>
<p>Choose a size<p>
<select id="size" name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
<option value="x-large">X-large</option>
</select>
<p> Choose a sauce <p>
<select id="sauce" name="sauce">
<option value="none">None</option>
<option value="marinara">Marinara</option>
<option value="alfredo">Alfredo</option>
<option value="ranch">Ranch</option>
<option value="bbq">BBQ</option>
</select>
<p> Choose a cheese<p>
<select id="cheese" name="cheese">
<option value="none">None</option>
<option value="mozzarelaa">Mozarella</option>
<option value="cheddar">Cheddar</option>
<option value="parmesan">Parmesan</option>
<option value="three cheese">Three-Cheese</option>
</select>
<p> Choose a meat <p>
<select id="meat" name="meat">
<option value="none">None</option>
<option value="Pepperroni">Pepperroni</option>
<option value="sausage">Sausage</option>
<option value="bacon">Bacon</option>
<option value="canadian bacon">Canadian Bacon</option>
<option value="chicken">Chicken</option>
<option value="salami">Beef</option>
<option value="anchovies">Anchovies</option>
</select>
<p> Choose a veggies <p>
<select id="veggies" name="veggies">
<option value="none">None</option>
<option value="onions">Onions</option>
<option value="green peppers">Green Peppers</option>
<option value="Red peppers">Red peppers</option>
<option value="Black olives">Mushrooms</option>
<option value="jalapenos">Jalapenos</option>
<option value="tomatoes">Tomatoes</option>
<option value="pineapple">Pineapple</option>
</select>
<p> Choose a crust <p>
<select id="crust" name="crust">
<option value="regular">Regular</option>
<option value="deep-dish">Deep-dish</option>
<option value="thin-crust">Thin Crust</option>
<option value="stuffed crust">Stuffed Crust</option>
<option value="gluten free">Gluten Free</option>
</select>
<p> Choose a drink <p>
<select id="drink" name="drink">
<option value="none">None</option>
<option value="rootbeer">Root Beer</option>
<option value="coke">Coke</option>
<option value="diet coke">Diet Coke</option>
<option value="dr pepper">Dr Pepper</option>
</select>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
Seems like you are not running the query.
// sql
$sql = "INSERT INTO order_info(drink,cheese,veggies,meat,sauce,crust,size)
VALUES('$value','$value2','$value3','$value4','$value5','$value6','$value7')";
// run query
mysqli_query($conn, $sql);
// or
$conn->query($sql);
You prepared string query but you are not executing it.
$sql = "INSERT INTO order_info(drink,cheese,veggies,meat,sauce,crust,size)
VALUES('$value','$value2','$value3','$value4','$value5','$value6','$value7')";
// run query with below mentioned function
mysqli_query($conn, $sql);
Then check your table. You will see the data saved.
I want to insert the selected status timestamps in the corresponding column which user has chosen from the menu.
Brief explanation
Start from the database i have created columns like snap below.
Initially user will insert the sonumber and status, Now i need database to update current time and date in the database, remaining column initialized to zero.
Next user will select update option, here user will enter so-number as well as new status(from dropdown). Now i need database to update the new status with current time and store in the particular status column.
Starting with insert page saved as "insert.html" and "insert.php" Respectively
<form id="form1" name="form1" method="post" action="insert.php" >
<p>
<lable>ENTER SO NUMBER</lable>
<input type="text" name="soid" id="soid" maxlength="6" required>
<p>
<lable>SELECT DEPARTMENT</lable>
<select type="text" name="dept" id="dept">
<option value="NGS Analysis">NGS Analysis</option>
<option value="E-Array">E-Array</option>
<option value="Micro-Array">Micro-Array</option>
<option value="NGS Data">NGS Data</option>
</select>
</p>
<p>
<lable>SELECT STATUS</lable>
<select type="text" name="status" id="status">
<option value="Sample Recived">Sample Recived</option>
<option value="Mol Bio Extraction">Mol-Bio Extraction</option>
<option value="Extraction QC">Extraction QC</option>
<option value="Library Prep">Library Prep</option>
<option value="Library QC">Library QC</option>
<option value="Sequencing">Sequencing</option>
<option value="Data check">Data Check</option>
<option value="Re-Sequencing">RE-Sequencing</option>
<option value="QC Check">QC Check</option>
<option value="Analysis Started">Analysis Started</option>
<option value="Analysis Completed">Analysis Completed</option>
<option value="Report">Report</option>
<option value="Outbound">Outbound</option>
</select>
</p>
<p><button><img src="http://brandonmadeawebsite.com/images/art/icons/insert_icon.png" height="50" />INSERT</button></p>
</form>
insert.php
<?php
$so = $_POST['soid'];
$dp = $_POST['dept'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "status";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
$result = mysqli_query($conn, "INSERT INTO $dbname.statusinfo (soid, dept ) VALUES ( '$so','$dp')") or die(mysqli_error($conn));
echo "Inserted sucessfully with So Number <u><b>$so</b></u> Corresponding Status is <u><b>$st</b></u>";
$conn->close();
?>
Now below update script which is saved as "update.html","update.php" respectively
<form action="update.php" method="post" name="form2">
<p>
<lable>ENTER SO NUMBER</lable>
<input type="text" name="soid" id="soid" required>
<p>
<lable>SELECT STATUS</lable>
<select type="text" name="status" id="status">
<option value="Sample Recived">Sample Recived</option>
<option value="Mol Bio Extraction">Mol-Bio Extraction</option>
<option value="Extraction QC">Extraction QC</option>
<option value="Library Prep">Library Prep</option>
<option value="Library QC">Library QC</option>
<option value="Sequencing">Sequencing</option>
<option value="Data check">Data Check</option>
<option value="Re-Sequencing">RE-Sequencing</option>
<option value="QC Check">QC Check</option>
<option value="Analysis Started">Analysis Started</option>
<option value="Analysis Completed">Analysis Completed</option>
<option value="Report">Report</option>
<option value="Outbound">Outbound</option>
</select>
</p>
<p><button><img src="http://icons.iconarchive.com/icons/icons8/windows-8/32/User-Interface-Available-Updates-icon.png" height="50" /> UPDATE</button></p>
</form>
update.php
<?php
$so = $_POST['soid'];
$st = $_POST['samplerecived'];
$st1 = $_POST['molbioextraction'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "status";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
switch ($st):
case $st:$result = mysqli_query($conn, "UPDATE statusinfo SET `samplerecived`= CURTIME() WHERE soid='$so' ") or die(mysqli_error($conn));
break;
case $st1:$result1 = mysqli_query($conn, "UPDATE statusinfo SET `molbioextraction`= CURTIME() WHERE soid='$so' ") or die(mysqli_error($conn));
break;
echo "Updated sucessfully with So Number $so Current Status is set to $st ";
$conn->close();
?>
Kindly help me to do so, if you guys need any more information feel free to ask.
Thanks in advance
If I understand your problem correctly, you only need the user selected fields to be inserted and the other to be either NULL or some default value.
You have the default value for all the fields set as CURRENT_TIMESTAMP. This is why all the fields are being assigned a default value of the current timestamp. Remove the default value, allow them to be NULL (or set some default value other than CURRENT_TIMESTAMP) and your problem will be solved.
I have a simple table like this: table. A form submits data to check in this table.
<form action="" method="GET">
From
<select name="startpoint" id="from">
<option >Hat Yai Airport</option>
<option >Pak Bara</option>
<option >Kohlipe</option>
</select>
To
<select name="endpoint" id="to">
<option >Pak Bara</option>
<option >Hat Yai Airport</option>
<option >Kohlipe</option>
</select>
<label for="Date">Date</label>
<input type="text" name="date_up" id="datepicker">
<h4 style="margin-top: 30px;">Passengers</h4>
Adults
<select name="adult" id="from">
<option >1</option>
<option >2</option>
<option >3</option>
<option >4</option>
<option >5</option>
</select>
< 12 years
<select name="juvenile" id="to">
<option >0</option>
<option >1</option>
<option >2</option>
<option >3</option>
<option >4</option>
</select>
< 7 years
<select name="kids" id="from">
<option >0</option>
<option >1</option>
<option >2</option>
<option >3</option>
</select>
< 3 years
<select name="child" id="to">
<option >0</option>
<option >1</option>
<option >2</option>
<option >3</option>
</select>
<input type="submit" value="Search" class="submit" name="submit">
And this is the initial i have made to check this table.
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hello";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_GET['submit'])) {
$date = $_GET['date_up'];
$startPlace = $_GET['startpoint'];
$endPlace = $_GET['endpoint'];
$adult = $_GET['adult'];
$juvenile = $_GET['juvenile'];
$kids = $_GET['kids'];
$child = $_GET['child'];
$totalPassenger = $adult + $juvenile + $kids + $child;
$query = "SELECT * FROM taxi WHERE date_up = '$date'";
$result = mysqli_query($conn, $query);
if (!$result) {
echo "Could not successfully run query from DB: " . mysqli_error();
exit;
}
if (mysqli_num_rows($result) == 0) {
$nodate = "No date matches for speedboat";
echo $nodate;
} else {
while ($row = mysqli_fetch_assoc($result)) {
$start = $row["startpoint"];
$end = $row["endpoint"];
$standards= $row["standards"];
$deluxe= $row["deluxe"];
//print_r($deluxe);
}
if (($start === $startPlace && $end === $endPlace) && (what can be the right query?????)) {
$success = "seats are found in taxi. total seat: user choses seat for $totalPassenger people";
echo $success;
}
}
}
We have total 10 taxi(standard 5 , deluxe 5). A taxi is for four people. I want to check if the total passenger is more than 4 people but less than 8 people and if this is true i want to check the standard or deluxe columns whether they both have at least two taxi(int 2) and return something. If the total passenger is more than 8 but less than 12 i need to check both columns if they have at least 3 taxi and this will go on until i reach 20 people. Because if the total passenger is more than 16 but less than 20 we need 5 taxi and this is our limit. If user provides more passenger after 20 we can return "not enough taxi for your query". How can i check this pattern.
I also need to return at least one column result. For example user searches with 7 passenger and i check and find that in deluxe column i have 2 taxi but in the standard column i have 1 taxi, so i need to return the deluxe column match result to the user(or to make the query more challenging we can return one standard taxi and one deluxe taxi right?). How can i implement this scenario. Please help me.
This code use ajax to fetch data without refreshing the page. and i have used PDO you can change it to mysqli
<?php
$db = new PDO('mysql:host=localhost; dbname=data','root','');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$results = $db->query('SELECT * FROM taxi');
//this is a function for creating a table
function availableTaxis($results){ ?>
<table>
<tr>
<th>ID</th>
<th>from</th>
<th>to</th>
<th>Available On</th>
<th>standard</th>
<th>deluxe</th>
</tr>
<?php foreach ($results as $value) : ?>
<tr>
<td><?php echo $value['taxi_id']; ?></td>
<td><?php echo $value['startpoint']; ?></td>
<td><?php echo $value['endpoint']; ?></td>
<td><?php echo $value['date_up']; ?></td>
<td><?php echo $value['standards'] .' ('.($value['standards'] * 4).' Passengers)'; ?></td>
<td><?php echo $value['deluxe'].' ('.($value['deluxe'] * 4).' Passengers)'; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php } ?> <!-- end of a function -->
<!-- this is another function for getting taxi routes -->
<?php function routes(){ ?>
<br>
<form>
From
<select name="startpoint" onchange="available(this.value,to.value)" id="from">
<option value="Hat Yai Airport">Hat Yai Airport</option>
<option >Pak Bara</option>
<option >Kohlipe</option>
</select>
To
<select name="endpoint" onchange="available(from.value,this.value)" id="to">
<option >Pak Bara</option>
<option >Hat Yai Airport</option>
<option >Kohlipe</option>
</select>
<p id="availabe_date"></p>
<div id="disable">
<label for="Date">Date</label>
<input type="date" name="date_up" value="<?php echo date('Y-m-d'); ?>" id="datepicker" onchange="available(from.value,to.value)">
<h4 style="margin-top: 30px;">Passengers</h4>
Adults
<select name="adult" id="adult">
<option >1</option>
<option >2</option>
<option >3</option>
<option >4</option>
<option >5</option>
</select>
12 years
<select name="juvenile" id="juvenile">
<option >0</option>
<option >1</option>
<option >2</option>
<option >3</option>
<option >4</option>
</select>
7 years
<select name="kids" id="kids">
<option >0</option>
<option >1</option>
<option >2</option>
<option >3</option>
</select>
3 years
<select name="child" id="child">
<option >0</option>
<option >1</option>
<option >2</option>
<option >3</option>
</select>
<br>
<p id="error" style="color:red"></p>
<br>
<input type="button" value="Search" onclick="results(datepicker.value,startpoint.value,endpoint.value,adult.value,juvenile.value,kids.value,child.value)" id="submit" name="submit">
</div>
</form>
<?php } ?><!-- end of a function -->
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
table,th,td{
border: 1px solid #000;
}
th{
width: 120px;
background-color: #000;
color: #fff;
text-transform: capitalize;
}
table {
border-collapse: collapse;
}
</style>
<script type="text/javascript">
//creating ajax
function available(from,to){
var query = "from="+from+"&"+"to="+to;
var data = new XMLHttpRequest();
data.open("POST","answer.php");
data.onreadystatechange = function(){
if(data.readyState === 4 && data.status === 200){
document.getElementById('availabe_date').innerHTML = data.responseText;
if(data.responseText.indexOf('is not avalaible') > -1){
document.getElementById('disable').style.display = 'none';
}else{
document.getElementById('disable').style.display = 'inline';
if(data.responseText.indexOf(datepicker.value) == -1){
document.getElementById('error').style.display = 'inline';
document.getElementById('error').innerHTML = 'There\'s no taxi on : '+datepicker.value +', from <span style="font-style:italic;text-decoration:underline">'+from+'</span> to <span style="font-style:italic;text-decoration:underline">'+to+'</span>';
document.getElementById('submit').style.display = 'none';
document.getElementById('res').style.display = 'none';
}else{
document.getElementById('error').style.display = 'none';
document.getElementById('submit').style.display = 'inline';
document.getElementById('res').style.display = 'inline';
//document.getElementById('error').innerHTML = 'There\'s no taxi on : '+datepicker.value;
}
}
}
}
data.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
data.send(query);
}
function results(date_up,startpoint,endpoint,adult,juvenile,kids,child){
var query = 'date_up='+date_up+'&'+'startpoint='+startpoint+'&'+'endpoint='+endpoint+'&'+'adult='+adult+'&'+'juvenile='+juvenile+'&'+'kids='+kids+'&'+'child='+child;
var data = new XMLHttpRequest();
data.open("POST","results.php");
data.onreadystatechange = function(){
if(data.readyState === 4 && data.status === 200){
document.getElementById('res').innerHTML = data.responseText;
}
}
data.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
data.send(query);
}
</script>
</head>
<body>
<?php availableTaxis($results);
routes();
?>
<div id="res">
</div>
</body>
</html>
answer.php file
<?php
$from = $_POST['from'];
$to = $_POST['to'];
$db = new PDO('mysql:host=localhost; dbname=data','root','');
$results = $db->prepare('SELECT * FROM `taxi` WHERE `startpoint` = ? AND `endpoint` = ?');
$results->execute(array($from,$to));
$rows = $results->fetchAll(PDO::FETCH_ASSOC);
$avalaible = "Avalaible on: <br>";
for ($i=0; $i < count($rows) ; $i++) {
$avalaible = $avalaible. $rows[$i]['date_up'].'<br>';
}
if(count($rows) > 0){
echo "$avalaible ";
}else{
echo "Taxi from <span style='font-style:italic;text-decoration:underline'>$from</span> to <span style='font-style:italic;text-decoration:underline'>$to</span> is not avalaible";
}
?>
results.php
<?php
$db = new PDO('mysql:host=localhost; dbname=data','root','');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$date = $_POST['date_up'];
$startPlace = $_POST['startpoint'];
$endPlace = $_POST['endpoint'];
$adult = $_POST['adult'];
$juvenile = $_POST['juvenile'];
$kids = $_POST['kids'];
$child = $_POST['child'];
$totalPassenger = $adult + $juvenile + $kids + $child;
$taxis = ceil($totalPassenger/4);
$results = $db->prepare('SELECT * FROM taxi where (startpoint = ? AND endpoint = ?) AND (date_up = ?) AND (standards >= ? OR deluxe >= ?)');
try {
$results->execute(array($startPlace,$endPlace,$date,$taxis,$taxis));
} catch (PDOException $e) {
echo $e->getMessage();
}
$rows = $results->fetchAll(PDO::FETCH_ASSOC);
$seats = ($rows[0]['standards']+$rows[0]['deluxe']) * 4;
$taxis = $rows[0]['standards']+$rows[0]['deluxe'];
echo 'Found '.$seats." seats, in $taxis taxis:<br>".($rows[0]['standards']*4).' Standards seats.<br>'.($rows[0]['deluxe']*4).' Deluxe seats.<br>For '.$totalPassenger.' passengers';
?>
This is the code. and for some reason I can't find out why it is not working.
As you can see I've added a test query to see if it affects any changes on my db but nope :(
The funny thing is that in another php script I have succeeded to connect the db and even added some records to it thru the php script. Can't find the problem, Thanks from advance.
BTW. as you can see I have already defined the var "month" as string "hey" and echo it in the end of the php script to see if it changes. but nothing is happen!!
<form action="" name="form" id="form">
<label>
Select the month which you want to display its days.
<select name="month" form="form" required>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
</label>
<input type="submit" name="update" value="Display" />
</form>
<?php
$month = "hey";
if(isset($_POST["update"]))
{
$month = $_POST["month"];
$query = "SELECT * FROM `days` WHERE `month`='{$month}';";
$conn = mysqli_connect("localhost","root","","db123");
$result = mysqli_query($conn,$query);
mysqli_query($conn,"INSERT INTO `days`(`month`,`day`) VALUES ('test','10');");
if($result)
{
die("Sorry!");
}
while($row = mysqli_fetch_row($result))
{
echo $month;
print_r($row);
}
mysqli_close($conn);
echo $month;
}
?
You didn't specified the method , its GET by default if.Change this line
if(isset($_POST["update"]))
to this
if(isset($_GET["update"]))
. Or if you want to use method as POST than just specify the method as POST
use the code below
<form action="" name="form" id="form" method="POST">
<label>
Select the month which you want to display its days.
<select name="month" form="form" required>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
</label>
<input type="submit" name="update" value="Display" />
</form>
<?php
$month = "hey";
if(isset($_POST["update"]))
{
$month = $_POST["month"];
$query = "SELECT * FROM `days` WHERE `month`='{$month}';";
$conn = mysqli_connect("localhost","root","","db123");
$result = mysqli_query($conn,$query);
mysqli_query($conn,"INSERT INTO `days`(`month`,`day`) VALUES ('test','10');");
if($result)
{
die("Sorry!");
}
while($row = mysqli_fetch_row($result))
{
echo $month;
print_r($row);
}
mysqli_close($conn);
echo $month;
}
?>
Hope this helps you
<?php
$mysqli = new mysqli("localhost", "root", "", "db123");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$month = "hey";
if(isset($_POST["update"]))
{
$month = $_POST["month"];
$res = $mysqli->query("SELECT * FROM `days` WHERE `month`='{$month}'");
mysqli_query($conn,"INSERT INTO `days`(`month`,`day`) VALUES ('test','10');");
while($row = $res->num_rows)
{
echo $month;
print_r($row);
}
mysqli_close($conn);
echo $month;
}
?>
i'm working on a php script wherein i must add certain score value at each row. I was able to display all the rows but i'm not sure on how would I able to store each of the given score in a variable and what query should I make to add all of them.
Here's my code
<?php
echo '<html>';
?>
<body>
<table border=0 cellspacing=0 cellpadding=0>
<?php
$connect = mysql_connect('localhost', 'root', '');
$db = 'labs';
$tb = 'comments';
$seldb = mysql_select_db($db, $connect);
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score" id="score" size="1">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</select></td></tr>';
echo'<br>';
}
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
if(isset($_POST['submit'])) {
//not sure if all the scores will be stored in here.
$id = $_POST['id'];
$query = mysql_query('insert query here');
}
?>
</table>
</body>
</html>
any suggestions are appreciated. Thanks in advance.:D
I think you need the id of each changed row (maybe as a hidden field for each row. Then just do a loop through all received rows and UPDATE each one.
You might also want to change all of your form field names to use the array format. This way it's easier to make your PHP loop throught them.
Sample row:
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score['.$i["id"].']" id="score" size="1">
<option value="5">5</option>
<option value="5">10</option>
<option value="5">15</option>
<option value="5">20</option>
<option value="5">25</option>
<option value="5">30</option>
<option value="5">35</option>
<option value="5">40</option>
<option value="5">45</option>
<option value="5">50</option>
</select></td></tr>';
Now just loop through the $_POST["score"] array and use the appropriate ID for your update.
foreach($_POST["score"] as $id => $value{
// ESCAPE your db values!!!!!
// query stuff with $value and $id
}
Also keep in Mind
mysql is deprecated! Use mysqli
Escape anything from outside sources like $_POST before use in SQL
You just needs to make an array of your drop down box like below,
while($i = mysql_fetch_assoc($query)) {
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score[".$i['com_id']."]" id="score" size="1">
<option valyue="5">5</option>
<option valyue="5">10</option>
<option valyue="5">15</option>
<option valyue="5">20</option>
<option valyue="5">25</option>
<option valyue="5">30</option>
<option valyue="5">35</option>
<option valyue="5">40</option>
<option valyue="5">45</option>
<option valyue="5">50</option>
</select></td></tr>';
echo'<br>';
}
and you can access it for all of your comments
<option valyue="5">50</option>
should be
<option value="5">50</option>
To send the value of a comment to database you need to add a ID of the comment
you should loop something like this.
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
echo '<input type="hidden" name="id" value="'.$i['com_id'].'">';
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score" id="score" size="1">
<option value="5">5</option>
<option value="5">10</option>
<option value="5">15</option>
<option value="5">20</option>
<option value="5">25</option>
<option value="5">30</option>
<option value="5">35</option>
<option value="5">40</option>
<option value="5">45</option>
<option value="5">50</option>
</select></td></tr>';
echo'<br>';
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
}
I guess the easiest way for you is the following (a mix of the other solutions and comments):
<?php
echo '<html>';
?>
<body>
<table border=0 cellspacing=0 cellpadding=0>
<?php
$x = 0;
$connect = mysql_connect('localhost', 'root', '');
$db = 'labs';
$tb = 'comments';
$seldb = mysql_select_db($db, $connect);
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
$x++;
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score_'.$x.'" id="score" size="1">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</select></td></tr>';
echo'<br>';
}
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
if(isset($_POST['submit'])) {
//not sure if all the scores will be stored in here.
$id = $_POST['id'];
for($y = 0;$y <= $x; $y++)
{
//This query is no sql injection save. Please add filters for productive uses!!
$query = mysql_query('UPDATE table_name SET score = '.$_POST["score_".$y].' WHERE id='.$id);
}
?>
</table>
</body>
</html>
Code is no tested!