Convert PHP MySQL to PHP PDO - php

I am trying to convert this PHP MySQL code from:
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["primary_cat"])) {
$query ="SELECT DISTINCT `secondary_cat` FROM student5 WHERE primary_cat = '" . $_POST["primary_cat"] . "'";
$results = $db_handle->runQuery($query);
?>
<option value="">Select State</option>
<?php
foreach($results as $state) {
?>
<option value="<?php echo $state["secondary_cat"]; ?>"><?php echo $state["secondary_cat"]; ?></option>
<?php
}
}
to PHP PDO as follows. I'm not sure what I am doing wrong. This is my code below.
$table = 'student5';
include('config.php');
if(!empty($_POST['primary_cat'])) {
$sqlQuerySecondaryCat = $dbh->query("SELECT DISTINCT secondary_cat FROM $table WHERE primary_cat = :primary_cat");
$sqlQuerySecondaryCat->execute(array(':primary_cat' => $_POST['primary_cat']));
?>
<option value="">Select State</option>
<?php
foreach($sqlQuerySecondaryCat as $secondaryCatRow) {
?>
<option value="<?php echo $secondaryCatRow["secondary_cat"]; ?>"><?php echo $secondaryCatRow["secondary_cat"]; ?></option>
<?php
}
}
I am getting the following error in my error_log PHP Warning: Invalid argument supplied for foreach()

First of all you need to use prepare, not query.
$sqlQuerySecondaryCat->prepare("SELECT .......
Secondly, you haven't done anything with the data stored in the object $sqlQuerySecondaryCat - you need to use fetch or fetchAll for the data.
$data = $sqlQuerySecondaryCat->fetchAll(PDO::FETCH_ASSOC);
foreach($data as $secondaryCatRow){
.... do something
Full code:
$table = 'student5';
include('config.php');
if(!empty($_POST['primary_cat'])) {
$sqlQuerySecondaryCat = $dbh->prepare("SELECT DISTINCT secondary_cat FROM $table WHERE primary_cat = :primary_cat");
$sqlQuerySecondaryCat->execute(array(':primary_cat' => $_POST['primary_cat']));
$rows = $sqlQuerySecondaryCat->fetchAll(PDO::FETCH_ASSOC); ?>
<option value="">Select State</option>
<?php foreach($rows as $secondaryCatRow) { ?>
<option value="<?php echo $secondaryCatRow["secondary_cat"]; ?>"><?php echo $secondaryCatRow["secondary_cat"]; ?></option>
<?php }
}

Related

Inserting selected value from populated mysql dropdown

I am trying to insert the selected value from a drop down that was populated from a reference table in my database. I followed a tutorial for a dynamic dropdown but now I would like to take the value and insert it. The problem is it keeps taking the echo the tutorial uses. Is there a way I can make that selected value a new variable? It currently inserts "< php echo $team_name"
<div>
<label>Home Team</label>
<select name="home_team" style="width:125px;>
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
mysqli_query($db, "SELECT * FROM team_name");
// loop
foreach ($results as $team_name) {
?>
<option value="<php echo $team_name["cid"]; ?><?php echo $team_name["team_name"]; ?></option>
<?php
}
?>
</select>
How I attempted to insert:
$db = mysqli_connect('localhost', 'root', 'root', 'register');
if(mysqli_connect_errno())
{
echo "failed" . mysqli_connect_error();
}
//var_dump($_POST);
$home_team = mysqli_real_escape_string($db, $_POST['home_team']);
$home_team = $home_team;
$query = "INSERT INTO game_table (home_team)
VALUES('$home_team')";
mysqli_query($db, $query);
//echo $query;
//echo $home_team;
//header('location: index.php');
please follow this.
<select name="home_team" style="width:125px;>
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($results)) {
?>
<option value="<php echo $row['cid']; ?>"><?php echo $row["team_name"]; ?></option>
<?php } ?>
</select>
may be this should work
Try this. There was a couple of missing " and ? in your code.
<select name="home_team" style="width:125px;">
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
foreach ($row = mysqli_fetch_assoc($results)) {
?>
<option value="<?php echo $row["cid"]; ?>">
<?php echo $row["team_name"]; ?>
</option>
<?php
}
?>
</select>

Set first value of dynamic HTML option to empty

<select id="section" name="section">
<?php
include("Nethost.php");
$section = "";
$yr = "";
$sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section");
while ($row = mysql_fetch_array($sql)){
$section = $row['section'];
$yr = $row['yrlvl'];
?>
<option value="">Select</option>
<option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'");
if(mysql_num_rows($result2) > 0) { ?>
selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option>
<?php } ?>
</select>
Above is the php code of the select option. Populated with data from database table, how can I set that the first value if empty. I tried adding a Select but this is the result.
The option select keeps repeating. What to do with this?
Try this
<select id="section" name="section">
<?php
include("Nethost.php");
$section = "";
$yr = "";
$sql = mysql_query("SELECT DISTINCT * FROM section ORDER BY yrlvl, section");
?>
<option value="">Select</option>
<?php
while ($row = mysql_fetch_array($sql)){
$section = $row['section'];
$yr = $row['yrlvl'];
?>
<option <?php $result2 = mysql_query("SELECT section FROM student WHERE idnumber = '$idnumber'");
if(mysql_num_rows($result2) > 0) { ?>
selected="selected" <?php } ?> value="<?php print $section; ?>"><?php print $yr; ?> - <?php print $section; ?></option>
<?php } ?>
</select>
Just move that option line "Select" out of your php code:
<select id="section" name="section">
<option value="">Select</option>
<?php
...your php code
?>
</select>

Throw select php variable into a form and convert it to a POST variable

I'm trying to use a drop down menu to load data from a select value. I want it passed into a php document in order to use the data that I need. What should I do? Thanks in advance.
This is my code for the select menu:
$sqlz = "SELECT * FROM content_temp1 WHERE user_uname='$uname'";
$resultz = mysql_query($sqlz);
$checkz = mysql_numrows($resultz);
$count = 0;
?>
<select name="ltemp">
<?php
while($count<$checkz){
$selectname=mysql_result($resultz,$count,"temp1_name");
?>
<option value="<?php echo "$selectname";?>"><?php echo $selectname;?></option>
<?php
$count++;
}
?>
</select>
<input type="submit" value="Load Template" class="ufbutton"><br></center>
</form>
and this is my php page
$uname = $_GET['username'];
$loadtemp = $_POST['ltemp'];
header("Location:editing1.php?username=$uname&tempname=$loadtemp");
it seems that you have use " inside " in this line :
<option value="<?php echo \"$selectname\";?>"><?php echo $selectname;?></option>
try this:
$sqlz = "SELECT * FROM content_temp1 WHERE user_uname='$uname'";
$resultz = mysql_query($sqlz);
$checkz = mysql_numrows($resultz);
$count = 0;
?>
<select name="ltemp">
<?php
while($count<$checkz){
$selectname=mysql_result($resultz,$count,"temp1_name");
?>
<option value="<?php echo \"$selectname\";?>"><?php echo $selectname;?></option>
<?php
$count++;
}
?>
</select>
Alternative :
$sqlz = "SELECT * FROM content_temp1 WHERE user_uname='$uname'";
$resultz = mysql_query($sqlz);
?>
<select name="ltemp">
<?php
while ($row = mysql_fetch_array($resultz, MYSQL_ASSOC)) {
$selectname=$row["temp1_name"];
?>
<option value="<?php echo \"$selectname\";?>"><?php echo $selectname;?></option>
<?php } ?>
</select>
Be careful this is ripe for sql injection without validating the input;
SQL Injection with GET

How to insert data in a dropdown list

We managed to get the drop down list menu however, we are having difficulties getting the data from sql. So far, this is what we got.
<select>
<option id="">--Select jobscope--</option>
<?php
$con=mysqli_connect("host","user","password","database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$getIT = mysqli_query("SELECT job_title FROM `job_details`");
while($viewIT = mysqli_fetch_array($getIT)) {
}
?>
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
</select>
Shouldn't be like this ? with tag inside WHILE LOOP
while($viewIT = mysql_fetch_array($getIT)) {
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
}
$query = "SELECT * FROM test_groups_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$dd .= "<option value='{$row['group_id']}'>{$row['group_name']}</option>";
}
Try this,
<option value="">--select--</option>
<?php
while($rec = mysql_fetch_assoc($result)) {
?>
<option value="<?=$rec['job_title']?>"><?=$rec['job_title']?></option>
<?php }
}?>
</select>
I am not from php background. Try this.
<?php
$query = "SELECT job_title FROM job_details";
$result = $mysqli->query( $query );
echo '<select id="domain_account" name="domain_account" class="txtBox">';
echo '<option value="">-select-</option>';
while ($row = $result->fetch_assoc()){
?>
<option value="<?php echo $row['job_title']; ?>"><?php echo $row['job_title']; ?></option>
<?php
}
echo "</select>";
?>
Better use PDO or MYSQLi . MYSQL* is depriciated
U need to use that <option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option> line b/w while loop
$getIT = mysql_query("SELECT job_title FROM `job_details`");
while($viewIT = mysql_fetch_array($getIT)) {?>
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
<?hp }?>

Get value of mysql database in select menu

I have issue with select menu on PHP. I tried to get mysql database to select menu. However, it displays none.
Here is my code:
default:
mysql_select_db($database_conn, $conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$Rsenroll = mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$row_Rsenroll = mysql_fetch_assoc($Rsenroll);
$totalRows_Rsenroll = mysql_num_rows($Rsenroll);
$courseid = $row_Rsenroll['courseid'];
$er_staffid = "";
break;
}
?>
<select name="courseid">
<option value="" SELECTED>Selected Course ID</option>
<?php
foreach( $Course as $course_id) {
if ( $course_id == $courseid) {
$selected = " SELECTED";
} else {
$selected = "";
}
?>
<option value="<?php echo $course_id; ?>"<?php echo $selected; ?>><?php echo $row_Rsenroll['courseid']; ?></option>
<?php
}
?>
</select>
Thank you for any help and advice.
Assuming courseid is being passed as a variable in the sending URL (file.php?courseid=COURSEID), I think this should do what you want:
This might clean up your script a little (though I switched it to mysql_fetch_array as I'm more familiar with that than mysql_fetch_assoc. Feel free to use assoc):
<?php
$cid = '6116';
?>
<select name="courseidMenu">
<option value="" SELECTED>Selected Course ID</option>
<?php
$query = mysql_query("SELECT * FROM tbl_enroll WHERE courseid = '$cid'", $conn)or die(mysql_error());
$total_rows = mysql_num_rows($query);
while($row = mysql_fetch_array($query)){
$courseId = $row['courseid'];
?>
<option value="<?=$courseId?>" ><?=$courseId?></option>
<?
}
?>
</select>
updated use this it is working on my portal <select>
<option value=''>Select Provider</option>
<?php
$server="server name";
$user="user name";
$password="password";
$database="database";
$conn=mysql_connect($server,$user,$password) or die("connection failed");
mysql_select_db($database,$conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$result= mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$n=mysql_num_rows($result);
if($n>0)
while($row=mysql_fetch_array($rs))
echo"<option value='$row['courseid']'>$row['courseid']</option>";
mysql_close($conn);
?>

Categories