How to fix mysqli_fetch_array? I use php, mysqli and html - php

i have a problem with my php code, it gives me the error of : Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in C:\xampp\htdocs\funcion2.php on line 69
I dont know how to solve it. i need some help pls, i need to finish the project.
<?php
//Conexion a la BBDD
include 'config.php';
session_start();
if($_REQUEST['cur']){$_SESSION['cur2']=$_REQUEST['cur'];}
$cur=$_SESSION['cur2'];
$str=$cur;
$cur=explode ('|', $str);
echo $cur[0];
if ($_REQUEST['alu']){$_SESSION['alu2']=$_REQUEST['alu'];}
$alu=$_SESSION['alu2'];
$str2=$alu;
$alu=explode ('|', $str2);
/*primera consulta*/
$query = 'select * from curso';
$res=mysqli_query($conexion, $query);
/*segunda consulta*/
$query2 = 'select * from alumnos where cod_curso=$cur[0]';
$res2=mysqli_query($conexion, $query2);
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
</head>
<body>
<form name="form1" method="post">
<div class="form-group col-md-3">
<label>Curso</label>
<select name="cur" onchange="this.form.submit() ;">
<option value="<?php echo $cur[1] ?>" ></option>
<?php
while ($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row[0]."|".$row[1]?>"><?php echo htmlentities($row[1]); ?>
</option>
<?php } ?>
</select>
</div>
<div class="form-group col-md-6">
<label>Alumnos</label>
<select name="alu">
<option value="<?php echo $alu[0]?>"</option>
<?php
while ($value=mysqli_fetch_array($res2))
{
?>
<?php foreach ($alu as $key => $value){ ?>
<option value="<?php echo $value[0]."|".$value[1]?>"><?php echo htmlentities($value[1]);?>
</option>
<?php } ?>
<?php } ?>
</select>
</div>
<input type="submit" name="enviar" value="Enviar" hidden />
</form>
<?php
echo "Tu curso es: ".$cur[1]."<br/>";
echo "El alumno es: ".$alu[1]."<br/>";
?>
</body>
</html>
There is the problem
.The error os mysql
My database have these 2 tables:
Table alumnos and Table curso
My tables and the problem
I modified the parameters without # and the second mysqli_fetch_array.

Why are you sending the connection to the while loop for alumnos?
while ($value=mysqli_fetch_array($conexion, $cur))
It's expecting a DB array result, not the connection.
*******EDIT
Now that I've taken a look, you still have errors in this section.
<?php
while ($value=mysqli_fetch_array($res2))
{
?>
<?php
foreach ($alu as $key => $value){
?>
<option value="<?php echo $value[0]."|".$value[1]?>">
<?php echo htmlentities($value[1]);?>
</option>
<?php } ?>
<?php } ?>
Why do you have a for each loop inside a while loop? Unless $value has data that you need to loop inside each row, it is already being looped by the while loop and you would not need the for each.

Related

Dropdown PHP with SQL with two different queries in one line inside dropdown > (Name, Number)

I want the dropdown to show the "client_code", "name" in one line. It almost works but not 100%. I am a beginner with php and SQL, can someone help me please?
Code that doesn't work
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$queryKlant = "SELECT naam FROM klant";
$queryKlantCode = "SELECT klant_code FROM klant";
$resultKlant=mysqli_query($mysqli,$queryKlant);
$resultKlantCode=mysqli_query($mysqli,$queryKlantCode);
while($row=mysqli_fetch_array($resultKlant) &&
$row2=mysqli_fetch_array($resultKlantCode) )
{
?>
<option><?php echo $row[0]. ", ". $row2[0];?></option>
<?php
}
?>
</select>
</form>
Code that only works with retrieving name in dropdown from database
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$queryKlant = "SELECT naam FROM klant";
$res=mysqli_query($mysqli,$queryKlant);
while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row[0]; ?></option>
<?php
}
?>
</select>
</form>
You can select more than one column from a table in the same select, and as both these columns live in the same table it makes producing this result much simpler.
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$sql = "SELECT naam, klant_code FROM klant";
$result = mysqli_query($mysqli,$sql);
while($row=mysqli_fetch_array($result)){
?>
<option><?php echo $row[0]. ", ". $row[1];?></option>
<?php
}
?>
</select>
</form>
You probably want to do this with your <option> tag as well rather than put the name and code in the visible portion
<option value="<?php echo $row[1];?>"><?php echo $row[0];?> </option>
And if you use mysqli_fetch_assoc() you can use the columns names so you know what you are putting where
while($row=mysqli_fetch_assoc($result){
<option value="<?php echo $row['klant_code'];?>"><?php echo $row['naam'];?> </option>

Change JSON value with PHP script

I have a JSON http://pastebin.com/index/LwAhi59r
And I have a PHP script in which I want to change my JSON value of each domain and template from each site. I would like to know what would be a good PHP code to change the values with an input and a selector in which you can choose which site you want to change it from. I got this php code I think it a good start but I don't know how to proceed.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Domain of Template veranderen met PHP script</h2>
<form action="process.php" id="form" method="post">
<select name="selector">
<option value="cms18.test.silverbee.nl">cms18.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">dmmd.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">opmaat.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">opmaatdebiteurenadvies.nl</option>
<option value="cms18.test.silverbee.nl">navbar.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">test18.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">huisartsplus.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">robertenrademaker.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">tilburg.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">cbk-groningen.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">getbusyinc.test.silverbee.nl</option>
</select>
Value:<input type="text" name="value">
<input type="submit" value="Send">
</form>
<br>
<?php
$select = $_POST['selector'];
//neem de json bestand array in php
$lijst= file_get_contents('list.json');
/*echo $lijst;*/
//neem de lijst json en decodeerd het json string. als true maakt hij er een array van.
$decoderen= json_decode($lijst, true);
/*echo $decoderen;*/
echo "<ul>";
foreach ($decoderen as $key => $value)
{
echo "<li>".$key."<ul>";
foreach ($value as $key1 => $value1)
{
echo "<li>".$key1.": ".$value1."</li>";
}
echo "</ul>"."</li>";
}
echo "</ul>";
/*------------------------------------------------------------------------------------------------------*/
/*foreach ($decoderen as $key => $value) {
if((string)$key-> == $this->put('')) {
$value->name="--";(string)$value->description="--";
}*/ This is the last code i was trying to use i found this in another site someone had same problem but i couldnt come out with this. BTW i am new in PHP.
?>
</body>
</html>
Try something like this:
<?php
$list = file_get_contents('list.json');
$list = json_decode($list, true);
$selector = $_POST['selector'];
if (isset($selector))
{
// overwrite the selected domain of the list with the new value if they are not empty
if (isset($_POST['domain']))
{
$list[$selector]['domain'] = $_POST['domain'];
}
if (isset($_POST['template']))
{
$list[$selector]['template'] = $_POST['template'];
}
// store the new json
file_put_contents('list.json', json_encode($list));
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Domain of Template veranderen met PHP script</h2>
<form action="process.php" id="form" method="post">
<select name="selector">
<?php foreach ($list AS $key => $value) : ?>
<option value="<?php echo $key; ?>"><?php echo $value['domain']; ?></option>
<?php endforeach; ?>
</select>
<input type="text" name="domain" placeholder="Domain">
<input type="text" name="template" placeholder="Template">
<input type="submit" value="Send">
</form>
</body>
</html>

php:process the select tag

this code run but when test it .. the data not correct i select data from database and i make it in array ad show it id by select tag but when select any id and submit .. example i select 5 and click on submit the record will delete is 2 not 5
<?php
require_once "config.php";
$qid="select id from info_user";
$arrayid=array();
$result=mysql_query($qid);
while($res=mysql_fetch_array($result)){
$arrayid[]=$res['id'];
}
var_dump($arrayid);
if(isset($_POST['sub'])){
$id=$_POST['id'];
$q="delete from info_user where id=$id ";
$qq=mysql_query($q);
if($qq){
echo "you delete record ";
}
else{
echo "error in deleting";
}}
?>
<html>
<head>
<title>delete</title>
</head>
<form action="delete.php" method="post">
<select name="id"><?php for($i=0;$i<count($arrayid);$i++){?>
<option value="<?php echo $i;?>"><?php echo $arrayid[$i];} ?></option></select> <br />
<input type="submit" name="sub" />
</form>
</html>
I think that problem is in value for the options.
Try changing option line to
<option value="<?php echo $arrayid[$i];?>"><?php echo $arrayid[$i];} ?></option></select> <br />
Your <option> markup is wrong. Make your markup like this;
<select name="id">
<?php for ($i = 0; $i < count($arrayid); $i++) { ?>
<option value="<?php echo $i; ?>"><?php echo $arrayid[$i]; ?></option>
} ?>
</select>
Tip: You'll find debugging easier if you indent your code. IDE's generally have this option. NetBeans is my favourite.
Instead of sending the ids, you're sending an array key associated to the id.
For instance, assume that: $arrayid = array(10, 11, 12);.
This is equivalent to: $arrayid = array(0 => 10, 1 => 11, 2 => 12);.
You'll see the options 10, 11 and 12, but you'll send either 0, 11 or 12, because that's what you're setting to the options values:
<select name="id">
<option value="0">10</option>
<option value="1">11</option>
<option value="2">12</option>
</select>
If you select the option "11", the SQL statement to delete an entry will be:
delete from info_user where id=1
I did not test this code and I'm assuming the ids are integers, but try it:
<?php
require_once "config.php";
$qid = "select id from info_user";
$arrayid = array();
$result = mysql_query($qid);
while( $res = mysql_fetch_array($result) ){
$arrayid[] = $res['id'];
}
if( isset($_POST['sub']) ){
$id = (int)$_POST['id']; // make sure it's an integer to avoid SQL injections
$q = "delete from info_user where id=$id";
$qq = mysql_query($q);
if( $qq ) {
$error = "you delete record ";
} else {
$error = "error in deleting";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>delete</title>
</head>
<body>
<?php
if( isset($error) ) :
echo '<p>', htmlspecialchars($error), '</p>';
elseif( !empty($arrayid) ) :
var_dump($arrayid);
endif;
?>
<form action="delete.php" method="post">
<select name="id">
<?php foreach($arrayid as $id): ?>
<option value="<?php echo (int)$id;?>">
<?php echo (int)$id; ?>
<?php endforeach; ?>
</option>
</select>
<br />
<input type="submit" name="sub" />
</form>
</body>
</html>

retrieve data and display in droplist (php/mysql)

can anyone tell me what is the error in this dam code i went crazy searching about the error but without any success
the error is that i do not get any data from the table why is that happening?? plz help me
index.php
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label for="searchByCountry">By Country</label>
<select name="searchByCountry" id="searchByCountry">
<option id="0">--select your country--</option>
<?php
require_once('connfor_lamelchameltest.php');
$getallCountries = mysql_query("SELECT country_name FROM country") or die("could not search for countries");
if(mysql_num_rows($getallCountries) > 0) {
while($viewallCountries = mysql_fetch_array($getallCountries)){
?>
<option id="<?php echo $viewallCountries['country_name']; ?> "></option>
<?php } ?>
<?php } ?>
</select>
</form>
</body>
</html>
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label for="searchByCountry">By Country</label>
<select name="searchByCountry" id="searchByCountry">
<option id="0">--select your country--</option>
<?php
require_once('connfor_lamelchameltest.php');
$getallCountries = mysql_query("SELECT country_name FROM country") or die("could not search for countries");
if(mysql_num_rows($getallCountries) > 0) {
while($viewallCountries = mysql_fetch_array($getallCountries)){
?>
<option value="<?php echo $viewallCountries['country_name']; ?> ">
<?php echo $viewallCountries['country_name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
</form>
</body>
</html>

handle info from select box

I have the following code:
<form name="votos" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php $categorias=mysql_query("SELECT * FROM categoria") or die (mysql_error()); ?>
<?php while($cat=mysql_fetch_array($categorias)){
echo "<h5>".$cat[0]." - ".$cat[1]."</h5>";
$nomeados=mysql_query("SELECT * FROM nomeados WHERE cod_categoria='$cat[0]'") or die(mysql_error());
?>
<div class="styled-select">
<select name="voto">
<option value=""></option>
<?php
while($nom=mysql_fetch_array($nomeados)){
$nomes=mysql_fetch_array(mysql_query("SELECT nome FROM logins WHERE cod_login='$nom[0]'")) or die (mysql_error());
?>
<option value="<?php echo $nom[0]; ?>"><?php echo $nomes[0]; ?></option>
<?php
}
?></select></div>
<?php }
?>
<input type="submit" class="botao" value="" name="submit" />
</form>
The code shows all the categories with the nomenies in a select box.
The thing is that the select box goes to post with the same name. I don't know how to get the information from all of the select boxes.
If you have more elements with the same name you can post them as an array
<select name="voto[]">
and than you get it in php
$votos = $_POST['voto']
foreach($votos as $voto){
//do what you need to do
}
and it's an array with all the values

Categories