Cannot Fetch Dropdown Value From Database - php

So I want to fetch my data from Database and be able to display and update it. Im on the part where I have to display a value from the db on a Dropdown but only the first option of the dropdown is being displayed.
Below is complete code.
<?php
mysqli_connect('localhost', 'root', '');
mysqli_select_db('storm');
$_GET['id'];
$ssh = $_GET['ssh'];
$_GET['provi'];
$_GET['impact'];
$_GET['advice'];
$_GET['date'];
$_GET['typhoon'];
$_GET['warning'];
?>
<html>
<body>
<div class="container">
<form action="edit.php" method="post">
<div class="form-group">
<label for="prov">Provinces</label><br>
<select id="prov" class="form-control" type="text" name="provi1">
<option value="Isabela"><?php echo $_GET['provi'];?></option>
<option value="La Union"><?php echo $_GET['provi'];?></option>
<option value="Pangasinan"><?php echo $_GET['provi'];?></option>
<option value="Ilocos Sur"><?php echo $_GET['provi'];?></option>
<option value="Ilocos Norte"><?php echo $_GET['provi'];?></option>
</select>
</div>
<div class="form-group">
<label>Date</label><br>
<input class="w3-input w3-border form-control" type="date" name="date" value="">
</div>
<div class="col-md-6">
<div class="form-group">
<label>Typhoon Name</label><br>
<input type="text" name="typhoon" value="<?php echo $_GET['typhoon']; ?>" class="form-control">
<input type="hidden" name="id" value="">
</div>
<div class="form-group">
<label>Warning #</label><br>
<input type="text" name="warning" value="<?php echo $_GET['warning']; ?>" class="form-control">
</div>
</div>
<div class="col">
<div class="form-group">
<input class="btnSubmit" type="submit" value="Update" name="submit" style="background-color: #408cff;">
<input class="btnSubmit" type="reset" value="Cancel" style="background-color: #de5959;">
</div>
</div>
</form>
<?php
if(isset($_GET['submit'])){
$id = $_GET['id'];
$ssh = $_GET['ssh'];
$muni = $_GET['muni'];
$impact = $_GET['impact'];
$advice = $_GET['advice'];
$date = $_GET['date'];
$typhoon = $_GET['typhoon'];
$warning = $_GET['warning'];
$query = "UPDATE twothree SET ssh='$ssh', muni='$muni', impact='$impact', advice='$advice', date='$date', typhoon='$typhoon', warning='$warning' WHERE id='$id'";
$data = mysqli_query($conn, $query);
if($data){
echo "Record Updated Successfully!";
}else{
echo "Record Not Updated.";
}
}
?>
I'm pretty sure that I am doing something wrong. Hope you guys figure it out for me. I'm new to this and I hope that I can learn from you guys. Thanks.

You can use something like this at basic level. this will retrieve all data from table and add a dropdown option to it.
<select class="" name="" required>
<option value="" selected disabled>Select a option</option>
<?php
$select_1 = $db->query("SELECT * FROM table");
while ($row_1 = $select_1->fetch_assoc()) {
?>
<option value="<?php echo $row_1['value']; ?>">
<?php echo $row_1['name']; ?>
</option>
<?php } ?>
</select>
where $db is your database connection and it must be mysqli connection.

You should do like this, to display the list of provinces.
<?php
$host = "localhost";
$user = "root";
$pwd = "";
$db = "storm";
$db_connection = new mysqli($host, $user, $pwd, $db);
if ($db_connection->connect_errno) {
printf("Connect failed: %s\n", $db_connection->connect_error);
exit();
}
//select provinces here
$provinces = $db_connection->query("Select * from provinces"); //change provinces according to your table name that you want to query.
?>
<div class="form-group">
<label for="prov">Provinces</label><br>
<select id="prov" class="form-control" type="text" name="provi1">
<?php
while ($row = $provinces->fetch_object()){
echo '<option value="'.$row->province_name.'">'.$row->province_name.'</option>'; //change province_name according to your fieldname.
}
?>
</select>
</div>
<?php
$db_connection->close();
?>

bro you need to loop data form your database to get continuous data out of your database you could use this.
<?php
$host = "localhost";
$user = "root";
$pwd = "";
$db = "storm";
$db_connection = new mysqli($host, $user, $pwd, $db);
if ($db_connection->connect_errno) {
printf("Connect failed: %s\n", $db_connection->connect_error);
exit();
}
//select provinces here
$query = mysqli_query($db_connection,"Select * from provinces");
?>
<div class="form-group">
<label for="prov">Provinces</label><br>
<select id="prov" class="form-control" type="text" name="provi1">
<?php
while ($row = mysqli_fetch_assoc($query)){
echo '<option value="'.$row["province_name"].'">'.$row["province_name"].'</option>'; //change province_name according to your fieldname.
}
?>
</select>
</div>
<?php
$db_connection->close();
?>
you code should look like this

Related

passing values from selected option in form

<form method="post">
<div class="form-group">
<label >Product</label>
<select class="form-control" name="product" id="supplier-select" >
<option value="">Select a Product </option>
<?php
$link = mysqli_connect();
if (!$link) {
die('Could not connect: ' . mysqli_connect_error());
}
$query="select id,name from product";
$result = mysqli_query($link,$query);
while($row = mysqli_fetch_array($result)){
echo "<option value=".$row['id']."name='product' >".$row['name']."</option>";
}
?></select>
</div>
<div class="form-group">
<label >Date</label>
<input type="date" name="date" class="form-control" id="" >
</div>
<div class="form-group">
<label >Sale Quantity</label>
<input type="text" name="quantity" class="form-control" id="">
</div>
<div class="form-group">
<label >Sale Price</label>
<input type="text" name="price" class="form-control" id="">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$id = $_POST['id'];
$product = $row['product'];
$date =$_POST['date'];
$quan = $_POST['quantity'];
$price = $_POST['price'];
$link= mysqli_connect();
if(!$link){
die ('connection unsuccessful'. mysqli_connect_error($link));
}
$sql = "INSERT INTO items_sale (sale_id, prod_id, date, sale_quantity, sale_price) VALUES ('$id','$product','$date','$quan','$price')";
if (mysqli_query($link, $sql)) {
exit();
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($link);
}
how do I pass in value from selected option from to insert query in this code here. i have using row[] but the value is not passing on in the from to my insert query when I selected. in past i have tried POST['id'] but it select the same value as the id before.
Try changing line
echo "<option value=".$row['id']."name='product' >".$row['name']."</option>";
to
echo "<option value=".$row['id'].">".$row['name']."</option>"
$row['product'] doesn't exist - try changing the beginning of your php code to
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$id = $_POST['id'];
$product = $_POST['product'];
...

Fatal error: Call to a member function query() on string in php [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
<div id="wrapper">
<div id="content">
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<label>Title:</label>
<input type="text" name="title" />
<label for="body">Body:</label>
<textarea name="body" cols=50 rows=10></textarea>
<label>Category:</label>
<select name="category">
<?php
$db='';
$query = $db->query("SELECT * FROM categories");
while($row = $query->fetch_object())
{
echo "<option value='".$row->category_id."'>".$row->category."</option>";
}
?>
</select>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
</div>
</div>
I'm getting error as
Fatal error: Call to a member function query() on string
I'm getting issue on this line
$query = $db->query("SELECT * FROM categories");
what is the issue in the code?
You need to connect to database first. After that you should run the query. Try following code
<div id="wrapper">
<div id="content">
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<label>Title:</label>
<input type="text" name="title" />
<label for="body">Body:</label>
<textarea name="body" cols=50 rows=10></textarea>
<label>Category:</label>
<select name="category">
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "your_database_name_here";
$conn = new mysqli($servername, $username, $password, $dbname);
$query = $conn->query("SELECT * FROM categories");
while($row = $query->fetch_object())
{
echo "<option value='".$row->category_id."'>".$row->category."</option>";
}
?>
</select>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
</div>
</div>

PHP form (sometimes) feeds blank entries into MySQL

I've tried creating a basic registration form (I'm pretty new to PHP).
The form works sometimes, but most of the times it's just sending blank entries into the MySQL database. Below is the code:
I have the following form:
<form action="#" method="post">
<h2 class="sub-heading-agileits">Participant 1</h2>
<div class="main-flex-w3ls-sectns">
<div class="field-agileinfo-spc form-w3-agile-text1">
<input type="text" name="name1" placeholder="Full Name" required="">
</div>
<div class="field-agileinfo-spc form-w3-agile-text1">
<select class="form-control" name="year1">
<option>Year</option>
<option value="1st Year">1st Year</option>
<option value="2nd Year">2nd Year</option>
<option value="3rd Year">3rd Year</option>
</select>
</div>
</div>
<div class="main-flex-w3ls-sectns">
<div class="field-agileinfo-spc form-w3-agile-text2">
<input type="text" name="phone1" placeholder="Phone Number" required="">
</div>
<div class="field-agileinfo-spc form-w3-agile-text2">
<input type="text" name="college1" placeholder="College" required="">
</div>
</div>
<div class="field-agileinfo-spc form-w3-agile-text">
<input type="email" name="email1" placeholder="Email" required="">
</div>
<h2 class="sub-heading-agileits">Participant 2</h2>
<div class="main-flex-w3ls-sectns">
<div class="field-agileinfo-spc form-w3-agile-text1">
<input type="text" name="name2" placeholder="Full Name">
</div>
<div class="field-agileinfo-spc form-w3-agile-text1">
<select class="form-control" name="year2">
<option>Year</option>
<option value="1st Year">1st Year</option>
<option value="2nd Year">2nd Year</option>
<option value="3rd Year">3rd Year</option>
</select>
</div>
</div>
<div class="main-flex-w3ls-sectns">
<div class="field-agileinfo-spc form-w3-agile-text2">
<input type="text" name="phone2" placeholder="Phone Number">
</div>
<div class="field-agileinfo-spc form-w3-agile-text2">
<input type="text" name="college2" placeholder="College">
</div>
</div>
<div class="field-agileinfo-spc form-w3-agile-text">
<input type="email" name="email2" placeholder="Email">
</div>
<div class="clear"></div>
<input type="submit" value="Submit">
<input type="reset" value="Clear Form">
<div class="clear"></div>
</form>
I'm sorry for the long form code.
This is the PHP code to post the data to the database:
$servername = "localhost";
$username = "fic";
$password = "fic201718";
$dbname = "fic201718";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name1 = $_POST['name1'];
$year1 = $_POST['year1'];
$phone1 = $_POST['phone1'];
$college1 = $_POST['college1'];
$email1 = $_POST['email1'];
$name2 = $_POST['name2'];
$year2 = $_POST['year2'];
$phone2 = $_POST['phone2'];
$college2 = $_POST['college2'];
$email2 = $_POST['email2'];
$sql = "INSERT INTO identitytheft (Participant1Name,Participant1Year,Participant1Phone,Participant1College,Participant1eMail,Participant2Name,Participant2Year,Participant2Phone,Participant2College,Participant2eMail) VALUES ('$name1','$year1','$phone1','$college1','$email1','$name2','$year2','$phone2','$college2','$email2')";
$conn->query($sql);
if (!empty($_POST['name1'])) {
echo ("<script type=\"text/javascript\"> alert('Successfully Registered'); </script>");
}
However, the form sometimes inserts absolutely blank data into the database. It sometimes works though.
One thing that I have noticed is, I do not get blank rows if there are no special characters in the responses. My columns are set to utf8_unicode_ci (all of the columns). Could there be something wrong here?
Please help?
If your POST variables aren't empty and the data you see is what you expect then it isn't your form and most like your query. Use var_dump to check your post variables after the form submission.
var_dump($_POST);
The first thing I will note is that you current approach is susceptible to SQL injection, so you'll want to clean up your code. Look up prepared statments http://php.net/manual/en/mysqli.quickstart.prepared-statements.php.
I'll clean up your current query, which should work for you. I obviously don't have your database setup, so I can test it. You don't have to format it like I did and I don't usually format it this way unless I'm debugging -- it helps align the column names with the values.
<?php
$servername = "localhost";
$username = "fic";
$password = "fic201718";
$dbname = "fic201718";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name1 = $_POST['name1'];
$year1 = $_POST['year1'];
$phone1 = $_POST['phone1'];
$college1 = $_POST['college1'];
$email1 = $_POST['email1'];
$name2 = $_POST['name2'];
$year2 = $_POST['year2'];
$phone2 = $_POST['phone2'];
$college2 = $_POST['college2'];
$email2 = $_POST['email2'];
$sql = "INSERT INTO identitytheft (
Participant1Name,
Participant1Year,
Participant1Phone,
Participant1College,
Participant1eMail,
Participant2Name,
Participant2Year,
Participant2Phone,
Participant2College,
Participant2eMail)
VALUES (
'".$name1."',
'".$year1."',
'".$phone1."',
'".$college1."',
'".$email1."',
'".$name2."',
'".$year2."',
'".$phone2."',
'".$college2."',
'".$email2."'
)";
$conn->query($sql);
if (!empty($_POST['name1'])) {
echo ("<script type=\"text/javascript\"> alert('Successfully Registered'); </script>");
}
?>
if(!empty($varname) && !empty($varname)){
sql statements over here within php code
}
you can add this similar type of code,as it works only when the input fields are not empty and does not post non empty data into your database

Using PHP and MySQL won't add form values to database table

Hi I created an rsvp form using bootstrap and PHP. I am new to PHP and database connections so I just wanted to confirm that I am doing it correctly. Am I using the correct syntax, is it safe from hacks (SQL injections), etc. Thanks.
Here is my PHP:
<?php
$servername = "rsvp.db";
$username = "******";
$password = "******";
$dbname = "rsvp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if(isset($_POST["submit"])){
$serverrsvp = test_input($_POST["formrsvp"]);
$serverattend = test_input($_POST["formattend"]);
$serverfullname = test_input($_POST["formfullname"]);
$serveremail = test_input($_POST["formemail"]);
$serverguests = test_input($_POST["formguests"]);
$serverguestnames = test_input($_POST["formguestnames"]);
$serverextras = test_input($_POST["formextras"]);
$serverrsvp = mysqli_real_escape_string($conn, $serverrsvp);
$serverattend = mysqli_real_escape_string($conn, $serverattend);
$serverfullname = mysqli_real_escape_string($conn, $serverfullname);
$serveremail = mysqli_real_escape_string($conn, $serveremail);
$serverguests = mysqli_real_escape_string($conn, $serverguests);
$serverguestnames = mysqli_real_escape_string($conn, $serverguestnames);
$serverextras = mysqli_real_escape_string($conn, $serverextras);
$sql = "INSERT INTO rsvp (dbrsvp, dbattend, dbfullname, dbemail, dbguests, dbguestnames, dbextras)
VALUES ('$serverrsvp', '$serverattend', '$serverfullname', '$serveremail', '$serverguests', '$serverguestnames', '$serverextras')";
if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('Thank you for your RSVP');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}
$conn->close();
}
?>
Here is my HTML:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="radio">
<label>Attending Wedding:<br>
<input type="radio" name="formrsvp" value="Yes" aria-label="..." checked>Yes, because I'm awesome! <br>
<input type="radio" name="formrsvp" value="No" aria-label="...">No, because I wish I was cooler... <br>
</label>
</div>
<div class="checkbox">
<label>Attending Friday Also?<br>
<input type="checkbox" value="Friday" name="formattend">Friday: Rehersal Dinner & Beach Party <br>
</label>
</div>
<div class="form-group">
<label for="formfullname">Full Name:</label>
<input required type="name" name="formfullname" class="form-control" placeholder="Please enter your full name">
</div>
<div class="form-group">
<label for="formemail">Email Address:</label>
<input required type="email" name="formemail" class="form-control" placeholder="Please enter your email address">
</div>
<div class="select">
<label>Any Extra Guests:</label>
<select name="formguests" class="form-control">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div class="textarea">
<label>Guest Names:</label>
<textarea name="formguestnames" class="form-control" rows="6" placeholder="Please enter the full name of any extra guests joining you..."></textarea>
</div>
<div class="textarea">
<label>Anything of Note:</label>
<textarea name="formextras" class="form-control" rows="10" placeholder="Please enter any specific information for the bride and groom, such as vegetarian guests, allergies, etc. If info is specific to a guest, please enter their name as well as info..."></textarea>
</div>
<br>
<button type="submit" value="Submit" name="submit" class="btn btn-default">Submit</button>
</form>

UPDATE sql field using Unique Key

I am using Unique Key for the field 'name', but when I update the record using edit file through admin, it gives me an error for duplicate entry. I can't remove unique key, it's required..
<!--update process-->
<?php
if(isset($_POST['submit']) and !empty($_POST['token'])){
print_r($_POST);
$name = $_POST['name'];
$size = $_POST['size'];
$linkt = $_POST['link'];
$seeds = $_POST['seeds'];
$leechs = $_POST['leechs'];
$active = $_POST['active'];
$query = "UPDATE tplus_torrentlist SET name = '$name', size = '$size', link = '$linkt', seeds = '$seeds', leechs = '$leechs', active = '$active'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
$error = mysqli_error($link);
if(empty($error)){
$_SESSION['flash'] = '<blockquote style="background: green; color: #fff">One record updated</blockquote>';
header('location:dashbord.php');
}
else{
$_SESSION['flash'] = '<blockquote style="background: green; color: #fff">Sorry cant updated this record</blockquote>';
header('location:dashbord.php');
}
}
?>
<!--fetch values from database according to id-->
<?php
$sql = "SELECT * FROM tplus_torrentlist WHERE id = $id limit 1";
$result = mysqli_query($link ,$sql) or die(mysqli_error($link));
$row = $result->fetch_array();
?>
<div class="container">
<div class="row clearfix">
<div class="col-md-8 col-md-offset-2">
<?php if(!empty($response)){echo $response;} ?>
<h3>Edit this torrent</h3>
<hr/>
<form role="form" class="form" action="edit.php?id=<?php echo $_GET['id']?>" method="POST">
<label>Name</label>
<input type="text" name="name" value="<?php echo $row['name']?>" class="form-control input-sm">
<label>Size</label>
<input type="text" name="size" value="<?php echo $row['size']?>" class="form-control input-sm">
<label>Link</label>
<input type="text" name="link" value="<?php echo $row['link']?>" class="form-control input-sm">
<label>Seeds</label>
<input type="text" name="seeds" value="<?php echo $row['seeds']?>" class="form-control input-sm">
<label>Leechs</label>
<input type="text" name="leechs" value="<?php echo $row['leechs']?>" class="form-control input-sm">
<label>Active</label>
<select name="active" class="form-control input-sm">
<?php if($row['active'] ==0){?>
<option value="0">Active</option>
<option value="1">InActive</option>
<?php }else{ ?>
<option value="1">InActive</option>
<option value="0">Active</option>
<?php } ?>
</select>
<br/>
<input type="hidden" name="token" value="<?php echo rand(100, 100000)?>">
<input type="submit" name="submit" value="update torrent" class="btn btn-info">
</form>
</div>
</div>
</div>
Any solution without removing unique key from the field 'name' ?
Your update query need to correct as following -
$query = "UPDATE tplus_torrentlist SET name = '$name', size = '$size', link = '$linkt', seeds = '$seeds', leechs = '$leechs', active = '$active' WHERE id = $id limit 1";

Categories