php mysql insert into statement - php

When running this code in the web page I do not get a confirmation that it is complete. I took the sql code directly from phpmyadmin sql query window. I would like a little help to this issue. it effects all the add pages.
id
$results=mysqli_query($con, "select * from Users where `userName` ='$userName'");
$row = mysqli_fetch_array($results);
$id = $row['id_cust'];
that is before this statement
php
if(isset($_POST['insert'])){
$artist = $_POST['artist'];
$place = $_POST['place'];
$hour = $_POST['hour'];
$minute = $_POST['min'];
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$price = $_POST['price'];
$open = $_POST['open'];
$time = $hour.':'.$minute;
$date = $year.'-'.$month.'-'.$day;
$result=mysqli_query($con,"insert into Concert values('$id','$artist','$date','$time','$place','$price','$open')");
if($result)
{
echo 'Values updated successfully';
}
}
html
<form name="addconcerts" method="post" action="addconcert.php" id="form">
<p>Please Fill out all information</p>
Artist:<input type="text" name="artist" /> <br />
Place:<input type="text" name="place" /><br />
Approximant start time<select name="hour">
<option value="">Hour</option>
<option value="01">01</option>
""""""""
<option value="24">24</option>
</select>
<select name="min">
<option value="">Minute</option>
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">35</option>
</select><br />
<select name="month">
<option value="">Month</option>
<option value = "01">January</option>
""""""""""""""""
<option value = "12">December</option>
</select>
<select name="day">
<option value="">Day</option>
<option value="01">01</option>
""""""""""""""""""
<option value="31">31</option>
</select>
<select name="year">
<option value="">Year</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select><br />
Price:<input type="text" name="price"><br />
Opening Act:<input type="text" name="open"><br><br>
<input type="reset" name="reset" value="Reset">
<input type="submit" name="insert" value="insert">
</form>
any help would be greatly appreciated.

Check Your Insert Query.
This is your Insert Query:
$result=mysqli_query($con,"insert into Concert values('$id','$artist','$date','$time','$place','$price','$open')");
And your Insert Query should look like this:
$result=mysqli_query($con,"insert into Concert (id, artist, date, time, place, price, open) values('$id','$artist','$date','$time','$place','$price','$open')");
Of course, you should REPLACE the necessary column names on the above provided code.

The $id variable is not declared. If PRIMARY_KEY and AUTO_INCREMENT, remove the variable.
$result = mysqli_query($con,"insert into Concert (id, artist, date, time, place, price, open) VALUES('$id','$artist','$date','$time','$place','$price','$open')");

Related

Values not being submitted from PHP HTML form [closed]

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.

Regarding PHP form

I am working on my project I want to store my multiple selected option value in the database but when I select more than one option at one time that time my only last selected values stored in DB please help my where am I am wrong?
Here is my Code:
<?php
require_once('contact_fun.php');
require_once('contact_banner_fun.php');
$obj = new contact_banner();
$crud = new contact_us();
$banner = $obj->get_data_banner();
if(!$banner){
echo "No Banner";
exit;}
$path = 'http://localhost/THE_VELVET_WALK/contact/';
$result = $crud->getData("SELECT * FROM tb_contactus");
foreach ($result as $res) {
$id = $res['id'];
$name = $res['name'];
$business = $res['business'];
$email = $res['email'];
$phone = $res['phone'];
$message = $res['message'];
$style = $res['style'];
}
if(isset($_POST['submit']))
{
$name = $crud->escape_string($_POST['name']);
$business = $crud->escape_string($_POST['business']);
$email = $crud->escape_string($_POST['email']);
$phone = $crud->escape_string($_POST['phone']);
$message = $crud->escape_string($_POST['message']);
$style = $crud->escape_string($_POST['style']);
$result = $crud->execute(" INSERT INTO tb_contactus(name, business, email,
phone, message, style, update_dt)VALUES ('$name','$business', '$email',
'$phone', '$message', '$style', now())");
}?>
<form action="" method="POST">
<input id="00N7F000001F2j6" name="name" maxlength="40" type="text"
class="materialize-input">
<input id="company" name="business" maxlength="40" type="text"
class="materialize-input">
<input id="email" name="email" maxlength="40" type="email"
class="materialize-input">
<input id="phone" name="phone" maxlength="40" type="text" maxlength="40"
class="materialize-input">
<select multiple id="00N7F000001F2kO" name="style" multiple="multiple" >
<option name="consulting" name="styling" value=""selected disabled>image
consulting</option>
<option value="1">styling</option>
<option value="2">hair & make-up</option>
<option value="3">designing clothes</option>
<option value="4">wedding makeover</option>
<option value="5">personal shopper</option>
<option value="6">corporate services</option>
</select>
</form>
you have to specify in the name of the select that it will be an array like :
<select multiple id="00N7F000001F2kO" name="style[]" multiple="multiple" >
<option value=""selected disabled>image consulting</option>
<option value="1">styling</option>
<option value="2">hair & make-up</option>
<option value="3">designing clothes</option>
<option value="4">wedding makeover</option>
<option value="5">personal shopper</option>
<option value="6">corporate services</option>
</select>
and then you can implode it if you want a string like this
implode(',',$_POST['style']);
EDIT :
change this
$style = $crud->escape_string($_POST['style']);
to
$style = $crud->escape_string(implode(',',$_POST['style']));
you need to use an array for multiple select options :
<select multiple id="00N7F000001F2kO" name="style[]" multiple="multiple" >
<option name="consulting" name="styling" value=""selected disabled>image
consulting</option>
<option value="1">styling</option>
<option value="2">hair & make-up</option>
<option value="3">designing clothes</option>
<option value="4">wedding makeover</option>
<option value="5">personal shopper</option>
<option value="6">corporate services</option>
</select>
this is how tou get the data into your database :
.php
if(isset($_POST['styling']) && !empty($_POST['styling'])){
$Col1_Array = $_POST['styling'];
print_r($Col1_Array);
foreach($Col1_Array as $selectValue){
//show selected
echo $selectValue."<br>";
}
}

php script won't connect MySQL

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;
}
?>

PHP Add value to each table row data

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!

Html form select blank to search all of the table where the other fields match

I have a form that searches a database and returns rows that match the fields specified. I want to give an option to the user that if the field is left blank, it won't matter what is is that column as long as the other fields match. Right now, if I leave a field 'blank' it will return every entry in the database.
Hair Color: <select name="hair">
<option value="hairall" selected="selected">--</option>
<option value="black" >Black</option>
<option value="brown">Brown</option>
<option value="blonde">Blonde</option>
<option value="white">White</option>
<option value="red">Red</option>
<option value="other">Other</option>
</select>
Height: <select name="height">
<option value="heightall" selected="selected">--</option>
<option value="smaller">Smaller</option>
<option value="small">Small</option>
<option value="average">Average - 70in</option>
<option value="tall">Tall</option>
<option value="taller">Taller</option>
</select>
Body Type: <select name="body">
<option value="bodyall" selected="selected">--</option>
<option value="skinny">Skinny</option>
<option value="average">Average - 194lb</option>
<option value="heavy">Heavy</option>
</select>
Ethnicity: <select name="ethnicity">
<option value="ethnicityall" selected="selected">--</option>
<option value="white">White</option>
<option value="black">Black</option>
<option value="asian">Asian</option>
<option value="hispanic">Hispanic</option>
<option value="middleeast">Middle Eastern</option>
<option value="other">Other</option>
</select><br/>
<center><input type="submit" value="Find Me" name="submit" ></center>
</form>
</div>
<div id="results">
<?php
$submit = $_GET['submit'];
$gender = $_GET['gender'];
$hair = $_GET['hair'];
$height = $_GET['height'];
$body = $_GET['body'];
$race = $_GET['ethnicity'];
//Hair All/Specific
if ($hair=='hairall'){
$newhair = "black' OR `hair`='brown' OR `hair`='blonde' OR `hair`='white' OR `hair`='red' OR `hair`='other";
}else
$newhair=$hair;
//Height All/Specific
if ($height=='heightall'){
$newheight = "smaller' OR `height`='small' OR `height`='average' OR `height`='tall' OR `height`='taller";
}else
$newheight=$height;
//Body Type All/specific
if ($body=='bodyall'){
$newbody = "skinny' OR `body`='average' OR `body`='heavy";
}else
$newbody=$body;
//Etnicity All/Specific
if ($race=='ethnicityall'){
$newrace = "white' OR `race`='black' OR `race`='asian' OR `race`='hispanic' OR `race`='middleeast' OR `race`='other";
}else
$newrace=$race;
//echo "$newhair <br/> $newheight <br/> $newbody <br/> $newrace<br/>";
require 'connect.inc.php';
$query = "SELECT * FROM `table` WHERE `gender`='$gender' AND `hair`='$newhair' AND `height`='$newheight' AND `body`='$body' AND `race`='$race' ORDER BY `id` DESC";
Put parenthesis around each of your values - i.e. ('$gender') instead of '$gender'. That will prevent your OR clauses from messing up your AND clauses, which is the problem you're having.
A better solution would be to remove the check from the query entirely if the field is left blank, but that would require rewriting about half your code. In any case, here's roughly how I would do it:
// Only accept the specific fields that are expected
$query_fields = array_intersect_key($_GET, array(
'gender'=>true,
'hair'=>true,
'height'=>true,
'body'=>true,
'ethnicity'=>true,
));
// Exclude blank fields
$query_fields = array_filter($query_fields, 'strlen');
$database = (require 'connect.inc.php'); // Get connection to database in this variable somehow
$where_parts = array();
foreach($query_fields as $k=>$v) {
$v = $database->real_escape_string($v);
$where_parts[] = "`$k` = '$v'";
}
if(!$where_parts)
die('No filters selected!');
$query = 'SELECT * FROM `table` WHERE '.implode(' AND ', $where_parts).' ORDER BY `id` DESC';

Categories