Why do I get a white row and not the value?
<select name="select">
<?php
$connessione = mysql_connect('localhost' , 'root', '') or die("Impossibile connettersi: " .mysql_error());
mysql_select_db("musica",$connessione);
$query = mysql_query("SELECT * FROM artisti_preferiti");
while($row = mysql_fetch_array($query))
{
?><option value="<?php echo $row['nome']; ?>"> <?php echo $row['cognome'];?></option>
<?php }?>
</select>
I suspect this being a DB connection issue, where I successfully tested this.
Consider the following:
Sidenote: Make sure that your settings are correct, including the DB name, and colum names.
DB connection file: (db_connect.php)
<?php
$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysqli = mysql_connect("$mysql_hostname", "$mysql_username", "$mysql_password");
if($mysqli->connect_errno > 0) {
die('Connection failed [' . $mysqli->connect_error . ']');
}
?>
PHP (example.php)
<select name="select">
<?php
include 'db_connect.php';
mysql_select_db("musica",$mysqli);
$query = mysql_query("SELECT * FROM artisti_preferiti",$mysqli);
while($row = mysql_fetch_array($query))
{
?><option value="<?php echo $row['nome']; ?>"> <?php echo $row['cognome'];?></option>
<?php }?>
</select>
On a final note, mysql_* functions are deprecated. Do consider using mysqli_* with prepared statements or PDO.
EDIT
MySQLi_* version
Sidenote: It is best using column names instead of SELECT * --- i.e.: SELECT nome, cognome
DB connection file: (db_connect_mysqli.php)
<?php
$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysql_dbname = 'xxx';
$mysqli = new mysqli("$mysql_hostname", "$mysql_username", "$mysql_password","$mysql_dbname");
if($mysqli->connect_errno > 0) {
die('Connection failed [' . $mysqli->connect_error . ']');
}
?>
PHP (example_mysqli.php)
<select name="select">
<?php
include 'db_connect_mysqli.php';
$query = $mysqli->query("SELECT * FROM artisti_preferiti");
while($row = mysqli_fetch_array($query))
{
?><option value="<?php echo $row['nome']; ?>"> <?php echo $row['cognome'];?></option>
<?php }?>
</select>
I changed your script so that we can debug
<?php
$lnk = mysql_connect('localhost' , 'root', '') or die("Impossibile connettersi: " .mysql_error());
mysql_select_db("musica",$lnk);
$q = 'SELECT nome,cognome FROM artisti_preferiti';
$result = mysql_query($q,$lnk);
$numRows = mysql_num_rows($result);
$rows = array();
while($row = mysql_fetch_array($result)) $rows[$row['nome']] = $row['cognome'];
/* show us this!! */
print_r($rows);
?>
<select name="select"><?php
foreach ($rows as $nome => $cognome) {
echo "<option value='$nome'>$cognome</option>";
}
?></select>
Related
I want to make a dropdown list and i use this codes:
<div class="selector">
<?php
include ("connect.php");
$db = new mysqli('localhost', $dbuser, $dbpass, $dbnam);
?>
<div class="label">Select Name:</div>
<select name="names">
<option value = "">---Select---</option>
$stmt = $db->prepare("SELECT `name` FROM `monitoare`");
$stmt->execute();
$stmt->bind_result($name);
while ($stmt->fetch()){
echo "<option value='$name'></option>";
}
$stmt->close();
?>
</select>
as index.php
and this:
<?php
$dbname = 'mydabase';
$dbuser = 'myuser';
$dbpass = 'mypass';
?>
as connect.php and after i launch this the drop stays just with ---select--- as an option
I reckon that you should do all of this using either MySQLi procedural or object oriented rather trying to do with prepared statements.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
You can save this in a 'connect.php' file and include it in the relevant file.
The solution to getting dropdown instead of --select-- without any errors is shown below
echo "<select name='names'>"
$sql = "SELECT `name` FROM monitoare";
$result =mysqli_query($db, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<option value=".$row['name'].">".$row['name']."</option>";
}
} else {
echo "No results found";
}
?>
For further reference check out http://php.net/manual/en/book.mysqli.php and also https://www.w3schools.com/php/php_mysql_insert.asp
Here's a (corrected) example, adapt to your code :
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$host = ""; /* your credentials here */
$user = ""; /* your credentials here */
$pwd = ""; /* your credentials here */
$db = ""; /* your credentials here */
/* connect to DBd */
$mysqli = mysqli_connect("$host", "$user", "$pwd", "$db");
if (mysqli_connect_errno()) { echo "Error connecting to DB : " . mysqli_connect_error($mysqli); }
$query = " SELECT `name` FROM `monitoare` ";
$stmt = $mysqli->prepare($query);
$stmt->execute();
$stmt->bind_result($name);
$stmt->store_result();
if ($stmt->num_rows > 0) { /* we have results */
echo"<select name=\"names\"><option value=\"\">---Select---</option>";
while($stmt->fetch()){
echo "<option value=\"$name\">$name</option>";
}
echo"</select>";
}
else
{ echo"[ no data ]"; }
?>
I would like to be able to delete a row using the delete-button I display at the end of each row. I need two queries to delete a person from my database completely since I have a n-m relationship between Persons and Functions.
The queries are as follows:
delete from `Persons` where `Person ID` = '1';
I would like to implement these queries using the delete-button provided in the actual code, how can I do this?
UPDATE:
I made changes according to what Kristian Hareland wrote, and it reloads but the person isn't deleted, what should be changed to make it work?
showall.php:
<table>
<thead>
<tr>
<?php
// Variables
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "CISV";
$dberror1 = "Could not connect to database: ";
$dberror2 = "Could not select database: ";
$dberror3 = "Could not execute query: ";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($dberror1 . mysql_error());
$select_db = mysql_select_db('CISV') or die ($dberror2 . mysql_error());
$query = "SELECT p.`Person ID`, p.`First Name`, p.`Last Name`, p.Gender, p.`Date Of Birth`, p.Email, p.`Phone Number`, c.Region, c.Country, p.`Delegation ID`
FROM Persons as p, Chapters as c
WHERE c.`Chapter ID`=p.`Chapter ID`
ORDER BY p.`Delegation ID";
$result = mysql_query($query) or die($dberror3 . mysql_error());
$row = mysql_fetch_assoc($result);
foreach ($row as $col => $value) {
echo "<th>";
echo $col;
echo "</th>";
}
?>
</tr>
</thead>
<tbody>
<?php
mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<?php
foreach($row as $key => $value){
echo "<td>";
echo $value;
echo "</td>";
}
?>
<td>Delete</td>
</tr>
<?php } ?>
DeletePerson.php:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "CISV";
$dberror1 = "Could not connect to database: ";
$dberror2 = "Could not select database: ";
$dberror3 = "Could not execute query: ";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($dberror1 . mysql_error());
$select_db = mysql_select_db('CISV') or die ($dberror2 . mysql_error());
$UserId = mysql_real_escape_string($_GET['id']);
if(isset($UserId)){
//DELETE QUERY
$Del = mysql_query("DELETE FROM `Persons` WHERE `Person ID` = '$UserId'");
if($Del){
$Flag = TRUE;
}
else{
$Flag = FALSE;
}
header("Location: /showall.php?delete=$Flag");
}
else{
die("Error");
}
I would use JavaScript and Ajax here.
Make a Html-button with an onclick function like delete_user();
delete_user() calls a .php file to validate the rights and execute some mysql-delete promts.
The .php file returns a boolean to the JavaScript.
You could catch that boolean up and inform the user about the result.
IMHO best way is create separate file functions.php
<?php
//functions.php
$id = filter_this($_POST[id]);
mysql_query("delete from Persons_Functions where `Person ID` = '$id');
?>
and send there via JQuery:
function deleteuser(id){
$.post("admin_action.php", {action:'delete_user',id:id}, function(data){
if(data.trim()=="done"){
console.log("OK!");
});
}}
Without JS You can use HTML (just change BUTTON to A):
<a href='script.php?action=delete&id=12'>DELETE</a>
and PHP:
$id = $_GET[id];//filter this value
mysqli_query($link, "DELETE FROM users WHERE ID='$id'"); //use mysqli
I would use the GET method for this
<?php foreach ($results as $result): ?>
Delete
<?php endforeach; ?>
At the top of index.php
if (isset($_GET['action']) && $_GET['action'] == 'delete') {
if (isset($_GET['id']) && intval($_GET['id']) != 0) {
$id = intval($_GET['id']);
$stmt = "delete from table where id='{$id}'";
}
}
Just use a link:
<tbody>
<?php
mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<?php
foreach($row as $key => $value){
echo "<td>";
echo $value;
echo "</td>";
}
?>
<td>Delete</td>
</tr>
<?php } ?>
</tbody>
DeletePerson.php:
<?php
$UserId = mysql_real_escape_string($_GET['id']);
if(isset($UserId)){
//DELETE QUERY
mysql_query("DELETE FROM Persons WHERE Person ID = '$UserId'");
mysql_query("DELETE FROM Persons_Functions WHERE Person ID = '$UserId'");
header("Location: /showall.php?flag=deleted");
}
else{
die("Error");
}
I want to show a combo box data from mysql table. Below is the code.
But it is not working.i am unable to find the error. can anyone please help me.
<select name="priority">
<?php
$server = 'localhost';
$user = 'root';
$pass = '1amShaw0n';
$db = 'shawon_logindb';
$db = new mysqli($server, $user, $pass, $db) or die("Unable to connect");
$query = "SELECT * FROM members";
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)){
?>
<option value="<?php echo isset($row["username"])?$row["username"]:''; ?>"> <?php echo isset($row["username"])?$row["username"]:''; ?></option>
<?php } ?>
</select>
Please help me to get the solution.
Conflicting mysql and mysqli.
There is two values in $db variable.
Remove isset inside <option>, because its only execute when its having data.
$conn = new mysqli($server, $user, $pass, $db) or die("Unable to connect");
$query = "SELECT * FROM members";
$result = mysqli_query($conn, $query);
while($row=mysqli_fetch_array($result, MYSQLI_ASSOC))
{
?>
<option value="<?php echo $row['username']; ?>"> <?php echo $row["username"]; ?></option>
<?php
}
?>
Use below code:
$db = new mysqli($server, $user, $pass, $db) or die("Unable to connect");
$query = "SELECT * FROM members";
$result = $db->query($query);
while($row=$result->fetch_assoc()){
?>
<option value="<?php echo isset($row['username']) ? $row['username'] : ''; ?>"><?php echo isset($row["username"]) ? $row["username"] : ''; ?></option>
<?php } ?>
Don't mix the mysql & mysqli functions.
I have a PHP script that dynamically shows the 'course' options from my database
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = '';
$con = mysqli_connect($db_host,$db_user,$db_pass, $db_name);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql = "SELECT courseID, name FROM courses";
$result = mysqli_query($con, $sql) or die("Error: ".mysqli_error($con));
while ($row = mysqli_fetch_array($result))
{
echo'<option value="'.$row['courseID'].'">'.$row['name'].'</option>';
}
?>
I have a dropdown in my HTML (scorecard.php) page.
<form> <select id="selectCourse" > <option value = "">Select Course</option></select></form>
I was wondering would anyone know a script or way of getting this data to display in my dropdown.
Thanks for any help
You have to run your while loop inside the select tag
Here is updated code
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = '';
$con = mysqli_connect($db_host,$db_user,$db_pass, $db_name);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql = "SELECT courseID, name FROM courses";
$result = mysqli_query($con, $sql) or die("Error: ".mysqli_error($con));
?>
<form>
<select id="selectCourse" >
<option value = "">Select Course</option>
<?php while ($row = mysqli_fetch_array($result))
{
echo'<option value="'.$row['courseID'].'">'.$row['name'].'</option>';
} ?>
</select>
echo'<option value="'.$row['courseID'].'">'.$row['name'].'</option>';
change it to
$courses[] = '<option value="'.$row['courseID'].'">'.$row['name'].'</option>';
and where u want it to be displayed
foreach($courses as $c){
echo $c;
}
I am querying results from a database, where more than one result should be queried. However, when I tried displaying the result of the query, only one result showed, so I tried to use a foreach function, but it's still not working. I'm beat, no idea what I'm doing wrong. Anyone have a good idea of what's going wrong?
Here's the MySQL query code:
<?php
//Database Information
$dbhost = "";
$dbname = "";
$dbuser = "";
$dbpass = "";
//Connect to database
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$filename = $_GET['filename'];
$new_captions = mysql_query("SELECT * from captions where image = 'http://math.stanford.edu/inc/img/PalmDrive.png' ORDER BY idnum DESC LIMIT 5");
while($rows = mysql_fetch_array($new_captions)){
$caption = $rows;
}
?>
And here's the foreach:
<?php foreach($caption as $rows) {?>
<div id="set_caption" style="width:<?php echo $caption['width'];?>px; height:<?php echo $caption['height'];?>px; left:<?php echo $caption['posleft'];?>px; top:<?php echo $caption['postop'];?>px;"><?php echo $caption['text'];?></div>
<?php } ?>
I think $caption is an array, so your code should be like this
while($rows = mysql_fetch_array($new_captions)){
$caption[] = $rows;
}
EDIT:
Your foreach loop is also wrong.
Your variable is $rows not $caption.
<div id="set_caption" style="width:<?php echo $rows['width'];?>px; height:<?php echo $rows['height'];?>px; left:<?php echo $rows['posleft'];?>px; top:<?php echo $rows['postop'];?>px;"><?php echo $rows['text'];?></div>
<?php } ?>
You have following mistakes.
$caption is not declare before.
use array_push or $caption[] = $rows; to make caption array.
Use $row variable in the foreach.
//Database Information
$dbhost = "";
$dbname = "";
$dbuser = "";
$dbpass = "";
//Connect to database
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$filename = $_GET['filename'];
$new_captions = mysql_query("SELECT * from captions where image = 'http://math.stanford.edu/inc/img/PalmDrive.png' ORDER BY idnum DESC LIMIT 5");
$caption = array();
while($rows = mysql_fetch_array($new_captions)){
$caption[] = $rows;
}
foreach($caption as $row) {
<div id="set_caption"
style="width:<?php echo $row['width'];?>px;
height:<?php echo $row['height'];?>px;
left:<?php echo $row['posleft'];?>px;
top:<?php echo $row['postop'];?>px;">
<?php echo $row['text'];?>
</div>
}