I want to insert the value of a selected 'select form' into my mysql database
this 'select from' is connected to database too
<label>Kategori</label>
<select class="form-control">
<?php include "config.php";
$kat = mysql_query("select * from kategori order by kode_kategori");
while ($hasil = mysql_fetch_array($kat)){
echo "<option value = '$hasil[kode_kategori]'>$hasil[nama_kategori]</option>";
}
?>
</select>
Image
Firstly, learn SQL. Learn the basics
To insert something into something, here is the syntax
$sql = "INSERT INTO mytable ('username', 'password') VALUES ('$_POST[username]', '$_POST[password]');";
You can insert value in database like below code:
<?php
if(isset($_POST['submit_button_name'])){
$insert_value = mysql_query("INSERT INTO mytable ('Column name1', 'Colum name2' , 'Column name3') VALUES ('$_POST['file_name1']', '$_POST['filed_name2']','$_POST['filed_name3']'");
if($insert_value){
echo "value insert successfully..."
}
else{
echo "error while insert data.."
}
}
?>
Related
I have a list code but it didn't get further)
<?
include("connect.phtml");
$r= mysql_query("SELECT name_goods FROM goods")
or die ("!1");
echo "<select name='product'>";
while($row=mysql_fetch_array($r))
{
echo "<option value='".$row['name_goods']."'>".$row['name_goods']."</option>";
}
mysql_close();
?>
You should put your name_goods id into your option value instead of the name.
Then you will need to insert a $_POST['product'] (which will be the selected value) to your database with the appropriated query.
Something like :
$good_id = $_POST['product']
$query = $pdo->prepare("INSERT INTO table(good_id) VALUES (".$good_id.")");
$query->execute();
The table deportes has 2 columns with 4 rows:
<?php
$sql = 'SELECT * FROM deportes';
$resul = mysqli_query($conexion, $sql);
$deportes = array();
while ($fila = mysqli_fetch_array($resul))
{
$deportes[] = $fila;
}
?>
The form with the select multiple options:
<select name="fan[]" multiple="multiple">
<?php
foreach ($deportes as $aficion)
{
echo "<option value='".$aficion['idD']."'";
echo " >{$aficion['nombreDep']} </option>\n";
}
?>
</select>
Get the values from the form
<?php
if (isset($_POST['fan']))
{
$sport = $_POST['fan'];
}
?>
Now this
<?php $sport = mysqli_real_escape_string($conexion, $sport); ?>
This way insert the values in another table
$idPersona = mysqli_insert_id($conexion);
$sql = "INSERT INTO mec(id,idD) VALUES ('$idPersona','$sport') ";
And the result is i get the value "0" in the field idD from table mec
If you print_r your $_POST['fan'], you will see that this is array. To get every value of array you should iterate over it, with for or foreach:
$idPersona = mysqli_insert_id($conexion);
foreach ($_POST['fan'] as $sport) {
echo $sport; // you will see that now it is string
$sql = "INSERT INTO mec(id,idD) VALUES ('$idPersona','$sport') ";
// execute your query
}
And of course you must move to prepared statements to protect your code from sql-injection. This question will give you a start.
We are having troubles with our php session.
Trying to add a patient to a database, linked to the doctor that is curing the patient.
We have a select from the existing doctors in the database. They show up in the dropdown list.
But when we are trying to send the selected doctor to the database (the php session), there is no addition to the database. All the other inputs (patient name, patient birth date, etc) are put in the database, except the data from the dropdown list.
Add_patient.php
Doctor:<br>
<select name="doctor">
<option value="">--Select--</option>
<?php
$config = parse_ini_file("divkey.ini.php", true);
include("connect/connect_mysql.php");
$opdracht = "SELECT * FROM gebruiker ORDER BY id";
$resultaat = mysql_query($opdracht);
while ($rij = mysql_fetch_array($resultaat)) {
$id = $rij['id'];
$name = $rij['name'];
$fname = $rij['fname'];
?>
<option value ="<?php $id;?>"><?php echo"$name $fname" ?></option>
<?php
} ?>
</select>
Session_add.php
$doctor = $_POST['doctor'];
# query
Our query from session_add.php works. Just the not for the $_POST['doctor'].
# query
$opdracht = "INSERT INTO patient ( `name`, `fname`, `geslacht`, `doctor`, `straatnaam`, `huisnummer`, `postcode`, `gemeente`, `telefoonnummer`, `patientnummer`, `land`, `bloedgroep`, `gsmnummer`, `geboortedatum`, `geboorteplaats`, `taal`, `nationaliteit`, `rijksregisternummer`, `huisarts` )
VALUES ('".$name."', '".$fname."', '".$geslacht."', '".$doctor."','".$straatnaam."' ,'".$huisnummer."' , '".$postcode."' , '".$gemeente."', '".$telefoonnummer."',
'".$patientnummer."','".$land."', '".$bloedgroep."', '".$gsmnummer."', '".$geboortedatum."', '".$geboorteplaats."', '".$taal."', '".$nationaliteit."','".$rijksregisternummer."', '".$huisarts."')";
# other values are not important, it's in Dutch and these values are sent to the database
# doing query
$result = mysql_query($opdracht) or die(mysql_error());
# we use or die(mysql_error())
The query is executed, a 0 (zero) is added to the database instead of the selected doctor.
<option value ="<?php echo $id;?>"><?php echo $name.$fname; ?></option>
Try this.
We found the problem, it was in the sql query. With var_dump($_POST)
I checked the values that were sent to the session, these were the right ones.
In the SQL query, there was '".$doctor."'
This was wrong, it should be '.$doctor.' (recognised as id)
Thanks for the help!
I am having a problem with updating some values in my database.
When I want to change a value using a drop down box, it automatically puts in the first value in the dropdown menu. What I want is to get the value that is already set in the database.
Here is my code:
<select name="vrijwilligerID">
<?php
$vrijwilligerID = $_POST["vrijwilligerID"];
$query = "SELECT voornaam, achternaam, vrijwilligerID FROM vrijwilliger;";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
echo "<option value=".$row['vrijwilligerID'].">".$row["voornaam"]." ".$row["achternaam"]."</option>";
}
?>
</select>
Does anyone know how to get this right?
Thank you in advance.
Based on the selected item you have to add selected attribute to the option.
try this
<select name="vrijwilligerID">
<?php
$vrijwilligerID = $_POST["vrijwilligerID"];
$query =" SELECT voornaam, achternaam, vrijwilligerID
FROM vrijwilliger;";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
if($row["vrijwilligerID"]==$vrijwilligerID)
echo "<option value=".$row['vrijwilligerID']." selected>".$row["voornaam"]." ".$row["achternaam"]."</option>";
else
echo "<option value=".$row['vrijwilligerID'].">".$row["voornaam"]." ".$row["achternaam"]."</option>";
}
?>
</select>
Its easy enough to resolve that using php, but using mysqls IF() function you can have the selected option directly. Using mysqls concat() you could get your desired result as well.
(ofcourse we're not using any postsvars in our scripts unsanitized are we ;) )
$iVrijwilligerid = filter_input(INPUT_POST, 'vrijwilligerID', FILTER_VALIDATE_INT, array("options"=> array("min_range"=>0, "max_range"=>999))) ;
if($iVrijwilligerid)
{
$sQry = <<<QRY
SELECT
CONCAT('<option value="', vrijwilligerID, '"',IF((SELECT vrijwilligerID FROM vrijwilliger WHERE vrijwilligerID=$iVrijwilligerid), ' selected="selected"', '' ),'>',voornaam, ,achternaam,'</option>') AS optItem
FROM
vrijwilliger
WHERE
your clause here
QRY;
$oResult = mysql_query( $sQry );
while($aRow=mysql_fetch_assoc( $oResult ))
{
echo $aRow['optItem'];
}
}
else
{
// nada
}
i am new here but i have a problem in inserting the id and the value of the checkboxes into my database here is the code of the form:
<?php
include('db.php');
$sql = "select * from sheet1 order by course_level asc";
$r = mysqli_query($dbc,$sql) or die(mysqli_error($dbc));
$co = '';
while($row = mysqli_fetch_array($r)) {
$co .= '<tr><td>'.$row['course_level'].'</td><td><input name="courses[]"
type= "checkbox" value = "'.$row['course_code'].'">'.$row['course_code'].'
</td> <td>'.$row['course_title'].'</td><td>'.$row['course_lecturer'].'
</td><input type=hidden name=cid[] value="'.$row['cid'].'">
</tr>';
}
?>
And this is the action code:
<?php
include('db.php');
if(isset($_POST['courses']))
echo 'lie';
else
echo 'true';
foreach($_POST['courses'] as $row=>$id){
$courses=$id;
$cid = $_POST['cid'][$row];
$sql = "insert into selected_courses values ('','$courses','$cid')";
$r = mysqli_query($dbc,$sql);
}
if($r)
echo 'done';
?>
thanks a lot.
You have several problems here, the main one being you are attempting to store two different reference values to the same row (course_code and cid) in your selected_courses table. You should really only store the primary key (cid?).
I'd suggest dropping the course_code column from your selected_courses table, remove the hidden input and structure your checkbox like this
<input type="checkbox"
name="courses[]"
value="<?php echo htmlspecialchars($row['cid']) ?>">
Then your INSERT query simply becomes
// Forget mysqli, move to PDO
$stmt = $dbc->prepare('INSERT INTO selected_courses (cid) VALUES (?)');
$stmt->bindParam(1, $cid);
foreach ($_POST['courses'] as $cid) {
$stmt->execute();
}