update and insert mysqli table - php

I'm trying to check if the leaguename name already exists if it does then check if it is higher than the existing score. if its higher then replace the quizscore else insert the leaguename and quizscore into the table league_quiz.
The code seem to insert the values in my table, but it do not seem update if the name is equal to previous and the score is higher?
I get this error:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given in
My code:
<?php
$name = (string)$_POST['name'];
$score = (string) $_POST['score'];
$link = mysqli_connect("mysql12.gigahost.dk","username","password","dirts_mysql");
$query = "SELECT leaguename, quizscore FROM league_quiz WHERE leaguename = '$name' AND quizscore < '$score'";
if(mysqli_num_rows($query)) {
mysqli_query($link,"UPDATE league_quiz SET quizscore = '$score'");
} else {
mysqli_query($link,"INSERT INTO league_quiz (leaguename, quizscore) VALUES ('$name', '$score')") or die(mysqli_error($link));
}
?>

mysqli_num_rows expects the result of a query and not the string of the query itself.
Firstly you must execute your query :
$result = mysqli_query($link,$query);
and then you can count the number of rows
$numRows = mysqli_num_rows($result);

Between those two:
$query = "SELECT leaguename, quizscore FROM league_quiz WHERE leaguename = '$name' AND quizscore < '$score'";
if(mysqli_num_rows($query)) {
Have you tried to write this code?
$result = mysqli_query($query);
and then use mysqli_num_rows on the result of the query?

Try the followowing:
$result = mysqli_query($query);
if (mysqli_num_rows($result)) {
...

Related

PHP - check if database column is empty

How do I check if a database column is empty?
I tried the following:
$check = "SELECT Name FROM data";
$name = mysqli_query($con, $check);
if(is_null($name) == true){
//run this code
}
My method was to select the column "Name" and then checking if the column is return NULL or not. For some reason, the if statement runs when I put "is_null($name) == false" but my database definitely has no data in it so surely it should return true since it should be NULL. What am I doing wrong here?
How can I check if the column of a database is empty using if statement?
Thanks.
you're treating the result as just the column.. it'd look more like
$check = "SELECT Name FROM data";
$result = mysqli_query($con, $check);
$row = mysqli_fetch_assoc($result)
if($row && $row["Name"] == null){
//run this code
}
You will need to use empty() function. change is_null to empty like this:
$check = "SELECT Name FROM data";
$name = mysqli_query($con, $check);
if(empty($name['Name'])){
//run this code
}
$check = "SELECT Name FROM data";
$name = mysqli_query($con, $check);
$nameAssoc = mysqli_fetch_assoc($name);
if(empty($NameAssoc['Name'])){
//run this code
}
You can modify your mysql query , check if any column is empty or not.
$check = "SELECT count(*) FROM data where Name is not null and Name != '' ";
$name = mysqli_query($con, $check);
if($name > 0 ){
//it means Name column is not null
}else{
// Name column is null
}

run insert query after select query mysql in php

i am trying to insert into multi table after select query return 0 (not found raws) select query working and insert query never done when submite "displayid" and there is no any syntax error
code:
<?php
if ($_POST["displayid"] == TRUE) {
$sqlid = "SELECT * FROM doc1 WHERE idnum ='$pidnum' AND stats='$ok'";
$result = mysqli_query($conn, $sqlid);
if (mysqli_num_rows($result) > 0) {
$sqlup = "UPDATE doc1 SET m_phone='$pm_phone', seen='$dataseen' WHERE idnum ='$pidnum'";
mysqli_query($conn, $sqlup);
$found = 1;
} else {
$found = 0;
$sqlfail = "INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date)
VALUES ('$pfname','$plname','$ptname','$pfuname','$pidnum','$pm_phone','$todaydate')";
$conn->query($sqlfail)
}
}
?>
Use this code:
$sqlfail = "INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date)
VALUES ('".$pfname."','".$plname."','".$ptname."','".$pfuname."','".$pidnum."','".$pm_phone."','".$todaydate."')";
make similar changes for update command as well
you actually have one error
$conn->query($sqlfail)
should be
$conn->query($sqlfail);
AND stats='$ok'";
i can't see a variable with this name i think you mean AND stats='ok'";

how to validate multiple inputs from database? [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 7 years ago.
I wan't to create a validation, check the database if the DATE, TIME and COTTAGE is already reserved/available. If any in one of the 3 is unavailable, it will save to the database.
Here is the code I made:
<?php
require_once("config.php");
require_once("includes/inputs.php");
//fetching the data of the cottage
$sql = "SELECT * FROM cottage WHERE id='$cottage'";
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
$name_cottage = $row["name"];
//fetching the data of the time
$sql = "SELECT * FROM rate WHERE id='$daytime'";
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
$daytime_name = $row["name"];
//validating whether the date, time and cottage is already available in the cottage
$validate = "SELECT * FROM reserve WHERE date = '$date' AND daytime = '$daytime' AND cottage = '$cottage'";
$qry = mysqli_query($link, $validate);
$getrow = mysqli_num_rows($qry);
if( $getrow > 0 ){
echo " $date, $daytime_name and $name_cottage has already been taken!";
return false;
}
else{
$sql = "INSERT into reserve (name, address, contact, email, date, time, ahc, chc, cottage, promo, total) values ('$name','$address','$contact', '$email', '$date', '$daytime', '$ahc', '$chc', '$cottage', '$promo', '$calc')";
$result = mysqli_query($link,$sql);
echo " $date, $daytime_name and $name_cottage is successfully reserved!";
return true;
}
?>
But i am having an error of:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\reservation\formup.php on line 22
and line 22 is
$getrow = mysqli_num_rows($qry);
but when i use this multiple select query:
$validate = "SELECT * FROM reserve WHERE date = '$date'";
$validate = "SELECT * FROM reserve WHERE daytime = '$daytime'";
$validate = "SELECT * FROM reserve WHERE cottage = '$cottage'";
it will save to the database but when the cottage have the same cottage on the database it won't save...
please help me to fix this....
By the documentation of msyql query:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
Link
So as i understand it the query is successful or not succesful true / false is returned else a mysqli_result object. So i think your query have no return value. Tried running the sql query on the db and have you check that the expected values are returned?
mysqli_num_rows() returns true on success and false on failure; for select, show etc. mysqli_query return result object.
I may need to see the full codes to get better idea whats wrong there but try this:
$qry = mysqli_query($link, "SELECT * FROM reserve WHERE date = '$date' AND daytime = '$daytime' AND cottage = '$cottage'");
$getrow = mysqli_num_rows($qry);
if( !$getrow == 0 ){
echo ".....taken";
}
else{
$sql = mysqli_query($link, "insert.....");
echo "...reserved";
}

Doesn't work "SELECT COUNT(*) FROM..." in PHP script

My function should return number of rows with email like '$email'. But whey return 0 all the time, although in database i have rows with email like I insert in variable '$email'. What could be the reason?
function checkMail($email){
$email = mysql_real_escape_string($email);
$sql = "SELECT COUNT(*) FROM users WHERE email='$email'";
return mysql_query($sql);
}
You aren't returning a result, you're returning a query resource:
function checkMail($email){
$email = mysql_real_escape_string($email);
$sql = "SELECT COUNT(*) as emailCount FROM users WHERE email='$email'";
$query = mysql_query($sql) or die(mysql_error()); // show error if one happens
return mysql_fetch_assoc($query);
}
This will return an associative array containing your results (if it succeeds), and you should be able to access your count by:
$res = checkMail('your#email.com');
$count = $res['emailCount'];
Side note:
mysql functions are deprecated, you should use mysqli or PDO syntax:
https://stackoverflow.com/a/13944958/2812842
function checkMail($email){
$email = mysql_real_escape_string($email);
$sql = "SELECT COUNT(*) FROM users WHERE email='$email'";
$resource=mysql_query($sql);
$row=mysql_fetch_array($resource);
return $row[0];
}
To fetch the count use:
mysql_query($sql)
$row = mysql_fetch_assoc($result);
return($row[0]);
The funny thing is that mysql_query return 0, which indicates query fail. Check the corresponding error message with:
echo mysql_error();

Database connectivity error

In the below code when I pass $id_num to check the id field in database it accepts but when I want to pass user id to check with database it shows the following error;
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in line no 12
Can anyone tell me where I'm going wrong.
code:
if(isset($_POST['user_mail']) && isset($_POST['user_pass']))
{
$var_1=$_POST["user_mail"];
$var_2=$_POST["user_pass"];
$result = mysqli_query($con,"SELECT * FROM jsrao_db2 WHERE user_mail=$var_1");
while($row = mysqli_fetch_array($result))
{
if(($row['user_mail']==$var_1) && ($row['user_pass']==$var_2))//compare user name and password with database value
echo "Welcome";
else
echo "Try Again";
}
change your query
$result = mysqli_query($con,"SELECT * FROM jsrao_db2 WHERE user_mail=$var_1");<br>
should be
$result = mysqli_query($con,"SELECT * FROM jsrao_db2 WHERE user_mail='$var_1'");<br>
user_mail is an string so enclose $var_1 in '$var_1'
Use Prepared Statements for cleaning up your code:
$result = false;
$stmt = $con->prepare("SELECT * FROM jsrao_db2 WHERE user_mail=?");
$stmt->bind_result($result);
$result = $stmt->bind_param("s", $var_1)->execute();
if ($result) {
//work with $result
}

Categories