Fetching data on textarea & dropdown doesn't work - php

I'm trying to do an update page, the data that I'm fetching are input texts, dates, dropdowns and a textarea.
The data on input texts & date works completely fine however, the data on my dropdowns and textarea wont appear.
I still can update it, the only problem here is it just won't appear. So I'm hoping if anybody can help me and see what I missed in my coding.
How my form looks like:
Solution:
I've found the solution to this question and below is my new html coding. There a few php coding that I edited at the dropdown and the dates.
Updated html code:
php code that are outside from my html:
<?php
session_start();
require('db.php');
include("auth.php");
$noID=$_REQUEST['noID'];
$query = "SELECT * from daftartempah where noID='".$noID."'";
$result = mysqli_query($con, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
?>
php code that are inside my html:
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$noID=$_REQUEST['noID'];
$trn_date = date("Y-m-d H:i:s");
$pemohon =$_REQUEST['pemohon'];
$trkhMula = $_REQUEST['trkhMula'];
$trkhAkhir =$_REQUEST['trkhAkhir'];
$n_program = $_REQUEST['n_program'];
$lokasi =$_REQUEST['lokasi'];
$n_anjuran = $_REQUEST['n_anjuran'];
$catatan = $_REQUEST['catatan'];
$status_hr = $_REQUEST['status_hr'];
$submittedby = $_SESSION["username"];
$update="update daftartempah set trn_date='".$trn_date."', pemohon='".$pemohon."', trkhMula='".$trkhMula."', trkhAkhir='".$trkhAkhir."', n_program='".$n_program."', lokasi='".$lokasi."', n_anjuran='".$n_anjuran."', catatan='".$catatan."', status_hr='".$status_hr."', submittedby='".$submittedby."' where noID='".$noID."'";
mysqli_query($con, $update) or die(mysqli_error());
$status = "Record Updated Successfully. </br></br>
<a href='Page8.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
Dropdowns:
<div class="form-group row text-left">
<label for="example-text-input" class="col-3 col-form-label"><b>Dept/Kelab/Anjuran: </b></label>
<div class="col-8">
<select class="form-control" name="n_anjuran" id="namaAnjuran">
<option selected name="" value="<?php echo $row['n_anjuran'];?>"><?php echo $row['n_anjuran'];?></option>
<?php
$sql = mysqli_query($con, "SELECT kd_dept, desc_dept From koddept");
$rows = mysqli_num_rows($sql);
while ($rows = mysqli_fetch_array($sql)){
echo "<option value='". $rows['kd_dept'] ."'>" .$rows['desc_dept'] ."</option>" ;
}
?>
</select>
</div>
</div>
Textarea:
<div class="form-group row text-left">
<label for="exampleTextarea" class="col-3 col-form-label"><b>Catatan: </b></label>
<div class="col-8">
<textarea class="form-control" rows="3" name="catatan" required><?php echo $row['catatan'];?></textarea>
</div>
</div>
My new form:
All questions are welcome, feel free to comment and ask, I'll try to help.

Related

PHP Posting an array from a form is not working

I have some checkboxes with printed values according to the database which are working fine.When I submit the form it gives me an error saying that the variable which I posted from the checkboxes array is not defined and that way I cant insert the values in the database.
The PHP code to post the array and insert in the database:
//The posts above don't matter because the first query works just fine
$candCurso = $_POST['curso'];
$query = "INSERT INTO tbl_candidatos(cand_nome, cand_datanasc, cand_nac, cand_nat, cand_gen, cand_morada, cand_codpost, cand_cidade, cand_email, cand_telefone, cand_telemovel, cand_tipodoc, cand_ndoc, cand_valdoc, cand_nif, cand_hablit, cand_sitprof, cand_grau, cand_instgrau, cand_datagrau, cand_instfunc, datainsc) VALUES ('$candNome','$candDataNasc','$candNac','$candNat','$candGen','$candMorada' ,'$candCodPost','$candCidade','$candEmail','$candTelefone','$candTelemovel','$candTipoDoc','$candNDoc','$candValDoc', '$candNif','$candHabLit','$candSitProf','$candGrau','$candInstGrau', ' $candDataGrau','$candInstFunc', '$dins')";
$result = mysqli_query($link, $query); // Executa a instrução MYSQL
if($result){
echo "<script>alert('I entered the result if');</script>";
$lastID = mysqli_insert_id($link);
foreach($candCurso as $value) {
echo "<script>alert('I entered the foreach if');</script>";
$data = date('Y-m-d');
$sql = "INSERT INTO tbl_professor_curso(cand_id, cur_id, data_admissao) VALUES ($lastID, '$value', '$data')";
$rr = mysqli_query($link, $sql); // Executa a instrução
}
The code doesn't run the foreach statement because of the variable $candCurso.
The form:
<!-- Message -->
<div class="col-md-12">
<!-- Form Group -->
<div class="form-group">
<label for="form_message">Select *</label>
<?php
$cursos = getCursos($link);
foreach ($cursos as $curso): ?>
<div class="checkbox">
<label >
<input type="checkbox" class="form-control" name="curso[]" value="<?= $curso['cur_id'] ?>"> <?= $curso['cur_nome'] ?>
</label>
</div>
<?php
endforeach;
?>
</div><!-- /End Form Group -->
</div><!-- /End Message -->
Any help is appreciated as I'm looking to fix this for hours.

Trying to send info to my database from a select tag

Been trying to get this PHP to send to my database but for some reason it won't work but it isn't giving me any errors either. The code probably isn't the prettiest only been working on PHP for 6 months so any help is much appreciated.
<form method="POST">
<div class="form-group">
<label for="exampleFormControlSelect1">Send Message to: </label>
<select class="form-control" id="slectrecipient" name="recipient">
<?php
include("conn.php");
$info = "SELECT FirstName, SecondName, id FROM PT_accounts WHERE NOT id='$accountid'";
$result4 = $conn->query($info);
if(!$result4){
echo $conn->error;
}
while($row4 = $result4->fetch_assoc()){
$recipientfirst = $row4['FirstName'];
$recipientsecond = $row4['SecondName'];
$recipientid = $row4['id'];
echo "<option value='$recipientid'> $recipientfirst $recipientsecond</option>";
}
if(isset($_POST['messagetext'])){
$currentdate = date("Y-m-d H:i:s");
$messagetext = $_POST['messagetext'];
$recipid = $_POST['recipient'];
echo $currentdate;
echo $messagetext;
echo $recipid;
$messageinsert = "INSERT INTO PT_Messages (SenderID, RecipientID, Date, Message)
VALUES ('$accountid', '$recipid', '$currentdate', '$messagetext') ";
$result5 = $conn->query($messageinsert);
if(!$result5){
echo $conn->error;
}else{
echo "<p> $messageinsert</p>";
echo "<p>Message Sent!</p>";
}
}
?>
</select>
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Message :</label>
<text class="form-control" id="messagetext" rows="3"></textarea>
</div>
<input type="submit" class="btn btn-primary btn-sm" value="Send">
</form>
I know there's two $row and $results but I've altered these in my actual to be different so I know that's not the issue. I'm unsure if it's the select tag with the option value that isn't written correctly. Or if I have to somehow set the dropdown menu selection as a PHP variable to then be sent to the database?
Thanks to user3783243
I didn't name the textarea but had set it as an id.
<input type="text" class="form-control" name="messagetext" id="messageid" rows="3">
I had it as id="messagetext"

Insert, Update, Search in MySql database using PHP

This is my first post in this forum, despite being a devoted follower for years now.
I have built a simple system that registers lot numbers and their locations within a MySQL database through a PHP form.
Then i have this other form called "Errata Corrige" that I use to find and edit eventual mistaken entries.
It's search criteria is an (UNSIGNED INT UNIQUE) value named "lotto" and everything works (worked) like a charm under this circumstances.
Now the thing got a little tricky.
I found out that lot numbers (lotto) for work purposes are not always unique values, there might be more than one entry with the same number.
No problem making the "Insert" form or various counters work under this new circumstances, but it got really tricky within the EDIT functions.
This is my PHP code: `
<?php
$id = "";
$settore = "";
$ubicazione = "";
$numero = "";
$lotto="";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// connect to mysql database
try{
$connect = mysqli_connect($host, $user, $password, $database);
} catch (mysqli_sql_exception $ex) {
echo 'Error';
}
// get values from the form
function getPosts()
{
$posts = array();
$posts[0] = $_POST['id'];
$posts[1] = $_POST['settore'];
$posts[2] = $_POST['ubicazione'];
$posts[3] = $_POST['numero'];
$posts[4] = $_POST['lotto'];
return $posts;
}
// Search
if(isset($_POST['search']))
{
$data = getPosts();
$search_Query = "SELECT * FROM mappa WHERE lotto = $data[4]";
$search_Result = mysqli_query($connect, $search_Query);
if($search_Result)
{
if(mysqli_num_rows($search_Result))
{
while($row = mysqli_fetch_array($search_Result))
{
$id = $row['id'];
$settore = $row['settore'];
$ubicazione = $row['ubicazione'];
$numero = $row['numero'];
$lotto = $row ['lotto'];
}
}else{
echo 'Lotto non presente in archivio';
}
}else{
echo 'Error';
}
}
// Insert
if(isset($_POST['insert']))
{
$data = getPosts();
$insert_Query = "INSERT INTO `mappa`(`settore`, `ubicazione`, `numero`, `lotto` ) VALUES ('$data[1]','$data[2]',$data[3], $data[4])";
try{
$insert_Result = mysqli_query($connect, $insert_Query);
if($insert_Result)
{
if(mysqli_affected_rows($connect) > 0)
{
$resInsert = "1 nuovo dato inserito correttamente!";
}else{
$resInsert = "Nessun dato inserito";
}
}
} catch (Exception $ex) {
echo 'Errore '.$ex->getMessage();
}
}
// Edit
if(isset($_POST['update']))
{
$data = getPosts();
$update_Query = "UPDATE `mappa` SET `settore`='$data[1]',`ubicazione`='$data[2]',`numero`=$data[3],`lotto`=$data[4] WHERE `id` = $data[0]";
try{
$update_Result = mysqli_query($connect, $update_Query);
if($update_Result)
{
if(mysqli_affected_rows($connect) > 0)
{
$resAgg = "1 dato aggiornato correttamente!";
}else{
$resAgg = "Nessun dato aggiornato!";
}
}
} catch (Exception $ex) {
echo 'Error Update '.$ex->getMessage();
}
} ?>
`
HTML:
<form action="mod.php" method="post" class="form-horizontal form-bordered" style="text-align:center">
<div class="form-group has-error" style="padding-top:30px">
<label class="col-xs-3 control-label" for="state-normal">ID</label>
<div class="col-lg-3">
<input type="text" name="id" placeholder="ID" class="form-control" value="<?php echo $id;?>"> </div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="state-normal">Settore</label>
<div class="col-md-6">
<input type="text" name="settore" placeholder="Settore" class="form-control" value="<?php echo $settore;?>"> </div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="state-normal">Ubicazione</label>
<div class="col-md-6">
<input type="text" name="ubicazione" placeholder="Ubicazione" class="form-control" value="<?php echo $ubicazione;?>"> </div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="state-normal">Numero</label>
<div class="col-md-6">
<input type="text" name="numero" placeholder="Numero" class="form-control" value="<?php echo $numero;?>"> </div>
</div>
<div class="form-group has-success">
<label class="col-md-3 control-label" for="state-normal">Lotto</label>
<div class="col-md-6">
<input type="text" name="lotto" placeholder="Lotto" class="form-control" value="<?php echo $lotto;?>"> </div>
</div>
<div style="padding-top:16px">
<!-- Insert-->
<button type="submit" name="insert" value="Add" class="btn btn-effect-ripple btn-primary">Inserisci</button>
<!-- Update-->
<button type="submit" name="update" value="Update" class="btn btn-effect-ripple btn-info">Aggiorna</button>
<a> </a>
<!-- Search-->
<button type="submit" name="search" value="Find" class="btn btn-effect-ripple btn-success">Cerca</button>
</div>
</form>
While the lot number was unique everything worked like a charm.
Now that there are multiple data with the same lot number the code became obsolete since the "search" function only shows the last (greatest ID) data.
I have tried to work around a loop and tell the function to search every ID where lotto = lotto but it didn't work.
A simple solution would be obviously searching through ID instead of lotto but that is a pretty crapy one, since the user only knows (and is interested in) Lot Numbers not the ID it was assigned during data insertion.
Then I tried to put two php functions into one page, the first that fetches data from Mysql into a PHP dropdown menu, telling it to show every ID that matches the search criteria (lotto):
<?php if (isset($_POST['submitted'])){
include ('../mysql_connect.php'); // connessione al database
$category = 'lotto';
$criteria = $_POST['criteria'];
$query = "SELECT * FROM mappa WHERE $category = '$criteria'";
$result = mysqli_query($dbcon, $query) or die('Impossibile reperire i dati');
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$idTab = $row['id'];
echo "<option>
$idTab </option>";
}
} // FINE if ?>
</select>
Fetching data from MySQL into the dropdown worked just fine, but I got stucked in the syntax trying to use this dropdown as a search criteria for my first function.
Every help would really be appreciated! Thank you in advance for your answers.
You said that lotto is unique. So how come you are able to insert multiple rows with the same lotto?
Remove the unique constraint from the lotto column.
Try the following:
$query = select lotto, group_concat(id) as ID numbers from mappa where lotto = 'user search number' group by lotto;
$result = $conn->query($query);
$rows = $result->num_rows;
$result->data_seek(0); //move to first row (which is the only one)
$row = $result->fetch_array(MYSQLI_NUM); //fetch array
$id_numbers_string = $row[1]; //store the values of the row's second column (which is number 1)
$id_numbers_separated_array = explode(",", $id_numbers_string); //create an array with the values in the string
for($i = 0; $i < count($id_numbers_separated_array); $i++){ //loop through created array
echo "ID: " . $id_numbers_separated_array[$i];
echo "<br>";
}
Also try to run the query in your database management system to see the results.

Fetching data from MySQL database to html drop-down list

I search for answers here but haven't found a solution.
I have also added picture of the error.
I want the data to go to the first drop-down list ( above the error)
I think the method I try to perform is also create drop-down list, am I correct?
<form name="message" action="" method="post" onsubmit="" accept-charset="utf-8">
<div class="form-group">
<label id="senderName">שם השולח:</label>
</div>
<div class="form-group">
<label for="to_user">מען:</label>
<select name="to_user" class="form-control">
<option value="pick">בחר מהרשימה</option>
<?php
$sql = \mysqli_query("SELECT name From users");
$row = mysqli_num_rows($sql);
echo "<select name='to_user'>";
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
}
echo "</select>" ;
?>
</select>
</div>
picture of the error
In MySQLi, the first parameter of a query needs to be the database connection. Also, there's no need to add a \ before the statement.
$sql = \mysqli_query("SELECT name From users"); should be $sql = mysqli_query($con, "SELECT name From users");
Note: replace $con with your database connection variable!
As you mentioned that you wanted the result from the database to go inside the select form, simply adjust your code to look like this:
<select name="to_user" class="form-control">
<option value="pick">בחר מהרשימה</option>
<?php
$sql = mysqli_query($con, "SELECT name From users");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
}
?>
</select>
<div class="row form-group">
<div class="col col-md-3">
<label for="email-input" class=" form-control-label"> Vehicle</label>
</div>
<div class="col-12 col-md-9">
<select name="car_id" id="car_id" class="form-control-label" >
<?php
$list = mysqli_query($conn,"SELECT * FROM `vehicle_registration` where `status`='0' ");
while ($row_ah = mysqli_fetch_assoc($list)) {
?>
<option value="<?php echo $row_ah['id']; ?>"><?php echo $row_ah['car_no']; ?></option>
<?php } ?>
</select>
</div>
</div>
<label><b>Select Steam: </b></label>
<select id="study">
<option value="" selected="selected" disabled="">---Selected---</option>
<?php
$query = "SELECT study FROM details";
$query_run = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($query_run)) {
echo "<option value='".$row['study']."'>".$row['study']."</option>";
}
?>
</select>

Form edit is blanking out my entries

So I've got a form to edit entries which is populating with what has already been entered from the database. When I make an edit, it is saving and redirecting me back to the listing page with no errors, but it's not changing anything. I'm guessing it's getting confused as to where to pull the values from.
This is the the SQL Query to populate the form with the values (this part works):
<?php
// query db
$gigid = $_GET['gigid'];
$con = mysqli_connect("***********","***********","***********","***********");
$result = mysqli_query($con, "SELECT * FROM gigs WHERE gigid=$gigid") or die(mysqli_error());
$row = mysqli_fetch_array($result);
mysqli_close($con);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$gig_name = $row['gig_name'];
$gig_type = $row['gig_type'];
$gig_date = $row['gig_date'];
$gig_customer = $row['gig_customer'];
$gig_venue = $row['venue_name'];
$gig_fee = $row['gig_fee'];
$gig_status = $row['gig_status'];
}
?>
This is an excerpt from the form:
<form class="form-horizontal" id="create-ticket" method='post' action='edit_gig_process.php? gigid=<?php echo $_GET['gigid']; ?>'>
<fieldset>
<legend>Edit Gig Information</legend>
<input type="hidden" class="input-xxlarge" id="gig_date_created" name="gig_date_created">
<input type="hidden" class="input-xxlarge" id="userid" name="userid">
<div class="control-group">
<label class="control-label" for="gigid">Gig ID</label>
<div class="controls">
<input type="text" name="gigid" disabled="disabled" value="<?php echo $_GET['gigid']; ?>" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="gig_name">Gig Name (Required)</label>
<div class="controls">
<input type="text" class="input-xxlarge" id="gig_name" value="<?php echo $row['gig_name']; ?>" name="gig_name">
</div>
</div>
This is an excerpt from the update query:
$gigid = $_GET['gigid'];
$sql= "UPDATE gigs set
gig_name='$gig_name',
gig_type='$gig_type',
gig_customer='$gig_customer',
gig_date='$gig_date_created',
gig_start_time='$gig_start_time',
gig_end_time='$gig_end_time',
gig_fee='$gig_fee',
gig_status='$gig_status',
venue_name='$venue_name',
venue_address='$venue_address',
venue_contact='$venue_contact',
WHERE
gigid='$gigid'";
header('Location: http://managegigs.com/cp/my-gigs.php');
mysqli_close($con);
You are not running a update query, your update is just a string.
After
$sql= "UPDATE gigs set
gig_name='$gig_name',
gig_type='$gig_type',
gig_customer='$gig_customer',
gig_date='$gig_date_created',
gig_start_time='$gig_start_time',
gig_end_time='$gig_end_time',
gig_fee='$gig_fee',
gig_status='$gig_status',
venue_name='$venue_name',
venue_address='$venue_address',
venue_contact='$venue_contact'
WHERE
gigid='$gigid'";
add:
mysqli_query($con,$sql);
also, at least change this:
$gigid = $_GET['gigid'];
add:
$gigid = mysqli_real_escape_string($gigid);
directly after to have it a little secured. Putting $_GET directly to DB is dangerous.
Uploadpart in complete:
$gig_name = $_POST['gig_name'];
// fetch all $_POST(ed) data
// and secure with
$gig_name = mysqli_real_escape_string($con,$gig_name);
$gigid = $_GET['gigid'];
$gigid = mysqli_real_escape_string($con,$gigid);
$sql= "UPDATE gigs set
gig_name='$gig_name',
gig_type='$gig_type',
gig_customer='$gig_customer',
gig_date='$gig_date_created',
gig_start_time='$gig_start_time',
gig_end_time='$gig_end_time',
gig_fee='$gig_fee',
gig_status='$gig_status',
venue_name='$venue_name',
venue_address='$venue_address',
venue_contact='$venue_contact'
WHERE
gigid='$gigid'";
mysqli_query($con,$sql);
header('Location: http://managegigs.com/cp/my-gigs.php');
mysqli_close($con);

Categories