I have a function to update a table but it does not work. My php error log does not report anything being wrong either so I am a bit stuck.
Here is the code:
if (count($errors) == 0) {
$query = "UPDATE wotd SET spanish='$spanish', english='$english', sex='$sex', example_es='$example_es', example_en='$example_en', description='$description' WHERE id=$wotd_id";
$_SESSION['message'] = "WORD updated successfully";
header('location: words.php');
exit(0);
My code was incomplete and therefore not posting the data to the database.
I needed to add another line below $query = "INSERT..."
This is my updated code:
if (count($errors) == 0) {
$query = "UPDATE wotd SET spanish='$spanish', english='$english', sex='$sex', example_es='$example_es', example_en='$example_en', description='$description' WHERE id=$wotd_id";
mysqli_query($conn, $query);
$_SESSION['message'] = "WORD updated successfully";
header('location: words.php');
exit(0);
}
Related
Every time i am trying to run the following PHP code on 000Webhost, i keep getting this error
-- mysqli_num_rows() expects parameter 1 to be mysqli_result.
The same code had been run successfully without errors on my localhost, XAMPP, i have looked through many examples and only found out that this error is caused by an error in the query, but as mentioned, the query works perfectly on my localhost.
The error is indicated in the code.
Any help would be appreciated.
<?php
session_start();
//decalre variables
$DeviceID ="";
$productID ="";
//connect to database
$db = mysqli_connect('localhost','id5655845_grocerywatch1234','123456','id5655845_grocerywatch1234');
//validate product id and device id are avaliable
if(isset($_POST['validate_user'])){
$DeviceID = mysqli_real_escape_string($db,$_POST['DeviceID']);
$productID = mysqli_real_escape_string($db,$_POST['productID']);
$query = "SELECT * FROM configuration WHERE DeviceID='$DeviceID' AND productID='$productID'";
$result1 = mysqli_query($db,$query);
echo $query;
//error indicated on the following line.
if(mysqli_num_rows($result1) == 1){
$_SESSION['DeviceID'] = $DeviceID;
$_SESSION['success'] = "You are now logged in";
header('location: register.php');
}
else{
echo "Device Not registered";
echo "Product Doesnt Exist";
}
}
I think your query is likely failing. The return value for mysqli_query is False on failure, otherwise it is mysqli_result. See docs here
Fix by properly formatting string:
...
$query = "SELECT * FROM configuration WHERE DeviceID='".$DeviceID."' AND productID='".$productID."'";
$result1 = mysqli_query($db,$query);
echo $query;
if ($result1 == false){
echo "Error has occurred!";
}
elseif (mysqli_num_rows($result1) == 1){
$_SESSION['DeviceID'] = $DeviceID;
$_SESSION['success'] = "You are now logged in";
header('Location: register.php');
}
else{
echo "Device Not registered";
echo "Product Doesnt Exist";
}
The query either returned no rows or is erroneus, thus FALSE is returned. Change it to
if (!$dbc || mysqli_num_rows($dbc) == 0)
Return Values
Returns TRUE on success or FALSE on failure. For SELECT, SHOW,
DESCRIBE or EXPLAIN mysqli_query() will return a result object.
I'm new to php.I'm trying to build a signup webpage in which if email entered doesn't exist it should insert the values entered.The code works fine and it returns successful when a new mail is entered.But the problem is when I check my database the new values are not inserted.Is there any mistake in my code?
Thanks in advance.
<?php
session_start();
if(isset($_POST['signup'])){
include_once("db.php");
$email=strip_tags($_POST['emailid']);
$username=strip_tags($_POST['username']);
$password=strip_tags($_POST['password']);
if($email==NULL || $username== NULL || $password==NULL){
print "Missing one of the fields";
}
else{
$email=stripslashes($email);
$username=stripslashes($username);
$password=stripslashes($password);
$email=mysqli_real_escape_string($db,$email);
$username=mysqli_real_escape_string($db,$username);
$password=mysqli_real_escape_string($db,$password);
$query = "SELECT * FROM user WHERE email='$email'";
$result = mysqli_query($db,$query);
if($result && mysqli_num_rows($result) > 0 )
{
echo "Account already exists.Please login";
}
else{
$sql="INSERT INTO user (ID,email,username,password) VALUES
(NULL,'$email','$username','$password')";
if($sql)
{
echo "Account created successfully.";
}
else
{
echo "Error";
}
}
}
}
?>
You are not executing the insert query, it should look like:
$sql="INSERT INTO user (ID,email,username,password) VALUES
(NULL,'$email','$username','$password')";
$sql= mysqli_query($db,$sql); ///You are missing this
Change from:
$sql="INSERT INTO user (ID,email,username,password) VALUES
(NULL,'$email','$username','$password')";
if($sql)
{
echo "Account created successfully.";
}
To:
$sql="INSERT INTO user (ID,email,username,password) VALUES
(NULL,'$email','$username','$password')";
if(mysqli_query($db,$sql))
{
echo "Account created successfully.";
}
You need to execute the 2nd query ($sql)
$sql="INSERT INTO user (email,username,password) VALUES
('$email','$username','$password')";
if(mysqli_query($db,$sql))
{
echo "Account created successfully.";
}
Remove the null INSERT value it's not needed and should be auto generated if auto-incremental index.
execute the $sql statement a a MySQLi_query and then use the result of that in the IF statement.
Bonus: Use mysqli_error($db) to feed you back errors you will encounter, such as:
mysqli_query($db,$sql) or die("error: ".mysqli_error($db));
Recently, I was making a login and signup page, where a mail gets sent if you sign up to confirm your account. I used MySQL for the account list, and have a 'confirm' column with 0 (unconfirmed) or 1 (confirmed).
Now, I created the following script to login, but it doesn't seem to be working. After executing this script, I see the "Processing...", then I see the "Such Sadness" part. I have checked in PHPMyAdmin that the account exists, and that it is actually confirmed. MySQL_Base is the file with the connection to the database, and that file is included in other files where the script works.
What is exactly the problem here?
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
$email = $_POST['email'];
$password = $_POST['password'];
include("mysql_base.php");
echo "Processing...<br>";
$sql = "SELECT * FROM pages_accounts WHERE confirm='1' AND email='".$email."' AND pass='".$password."' LIMIT 1";
if($result = mysqli_query($conn, $sql) && $result->num_rows > 0){
echo "FOUND UR ACCOUNT. MUCH HAPPINESS. SUCH GLAD. WOW REDIRECTING...";
} else {
echo "SUCH SADNESS. NO ACCOUNT WITH SUCH NAME. WEIRD RETURN. WOW. WHY NOT <a href='loginSignup.php'>SIGNUP</a>?";
}
Replace these 3 lines:
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($query);
if(mysql_fetch_array($query) !== false){
with this one:
if($result = mysqli_query($conn, $sql) && $result->num_rows > 0){
UPDATE Last version after discussion in chat:
if($result = mysqli_query($conn, $sql)) {
if ($result->num_rows > 0){
echo "FOUND UR ACCOUNT. MUCH HAPPINESS. SUCH GLAD. WOW REDIRECTING...";
} else {
echo "SUCH SADNESS. NO ACCOUNT WITH SUCH NAME. WEIRD RETURN. WOW. WHY NOT <a href='loginSignup.php'>SIGNUP</a>?";
}
} else {
echo "Errormessage: %s\n". mysqli_connect_error();
}
table name -- breaking_news
field name -- status
I have created a single page and passes id from link click.
Now I check if status not empty and status equal to Inactive
then update status = Active Else update status Inactive
but it is not working properly.
It Only Works For If Condition.
The ELSE Condition of Code is Not Working . plz suggest me how to write in if else properly...
<td><img src="img/active.png" width="24" height="24" border="0" title="Active" /></td>
<?php
$Admin = new admins;
$sql = "SELECT status FROM breaking_news WHERE id=".mysql_real_escape_string($_GET['status_active']);
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if(!empty($row) && $row['status']=='Inactive')
{
mysql_query("Update breaking_news SET status='Active' WHERE id=".mysql_real_escape_string($_GET['status_active']));
$_SESSION['message'] = "Status Activated Successfully";
header("Location:breaking_news.php");
exit;
}
else
{
mysql_query("Update breaking_news SET status='Inactive' WHERE id=".mysql_real_escape_string($_GET['status_inactive']));
$_SESSION['message'] = "Status De-Activated Successfully";
header("Location:breaking_news.php");
exit;
}
?>
Try this code
">
<?php
$Admin = new admins;
$sql = "SELECT status FROM breaking_news WHERE id=".mysql_real_escape_string($_GET['status_active']);
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if(!$row && $row['status']=='Inactive')
{
mysql_query("Update breaking_news SET status='Active' WHERE id=".mysql_real_escape_string($_GET['status_active']));
$_SESSION['message'] = "Status Activated Successfully";
header("Location:breaking_news.php");
exit;
}
else
{
mysql_query("Update breaking_news SET status='Inactive' WHERE id=".mysql_real_escape_string($_GET['status_inactive']));
$_SESSION['message'] = "Status De-Activated Successfully";
header("Location:breaking_news.php");
exit;
}
?>
you have just to ask if $row was have more or not, be cause mysql_fetch_assocreturn false if it's empty look to this manual : mysql_fetch_assoc PHP
you have just do not use empty()
Or like that :
if($row = mysql_fetch_assoc($result))
{
if($row['status']=='Inactive')
{
mysql_query("Update breaking_news SET status='Active' WHERE id=".mysql_real_escape_string($_GET['status_active']));
$_SESSION['message'] = "Status Activated Successfully";
header("Location:breaking_news.php");
exit;
}
}
else
{
mysql_query("Update breaking_news SET status='Inactive' WHERE id=".mysql_real_escape_string($_GET['status_inactive']));
$_SESSION['message'] = "Status De-Activated Successfully";
header("Location:breaking_news.php");
exit;
}
If you have not found a row, there is nothing to update...
You may wish to do something like this:
<?php
if (!empty($row)) {
if ($row['status'] == 'Inactive') {
//update to active
}
else if ($row['status'] == 'Active') {
//update to inactive
}
}
also you've got a typo in the second call to $_GET array: $_GET['status_inactive']. You should be updating the same row but with different status value.
EDIT
#toto21 I don't have enough reputation to comment on your answers but no, your answer is wrong. As I mentioned at the top - if there is no row fetched then there is nothing in the db for you to update, so your else statement makes no sense.
The following is my code that I have written for not inserting same data
I would like if the record exist in mysql then it should show me error message that the record already exist the else part should insert record to database but it not working
can any one help me plz
the help would be highly appreciated
function addcontact()
{
if(isset($_POST['addContact']))
{
$officeName = strip_tags($_POST['office_name']);
$contactName = strip_tags($_POST['contactName']);
$contactNo = strip_tags($_POST['contactNo']);
$digitalNo = strip_tags($_POST['digitalNo']);
$mobileNo = strip_tags($_POST['mobileNo']);
$check="SELECT * FROM contacts WHERE office_name = '$officeName'";
if(mysql_num_rows($check) != 0)
{
echo "Already in Exists<br/>";
}else
{
$sql = mysql_query("INSERT INTO contacts (office_name, contact_no,
digital_no, mobile_no) VALUES
('$contactName','$contactNo','$digitalNo','$mobileNo')") or die(mysql_error());
if($sql)
{
header("Location: index.php?admin&done"); exit;
}
else
{
header("Location: index.php?admin&failed"); exit;
}
}
}
}
you did mistake here.
$check="SELECT * FROM contacts WHERE office_name = '$officeName'";
if(mysql_num_rows($check) != 0)
{
echo "Already in Exists<br/>";
}
just add mysql_query like
$check=mysql_query("SELECT * FROM contacts WHERE office_name = '$officeName'");
if(mysql_num_rows($check) != 0)
{
echo "Already in Exists<br/>";
}
or you can also use like
$name=$_POST['username'];
$q="select * from login where name='$name' ";
$rs=mysql_query($q);
if(mysql_fetch_row($rs)>0)
{
echo "already exist";
}
else
{
$msg="done";
}
Add the ON Duplicate KEY Update. This way you don't need to check if the record already exists, which means you don't need an extra select query just to check. If it exists, nothing happens.
INSERT INTO contacts (office_name, contact_no, digital_no, mobile_no)
VALUES ('$contactName','$contactNo','$digitalNo','$mobileNo')
ON DUPLICATE KEY UPDATE office_name = office_name
And set the office_name to be the primary key or a unique index.
There is missing one step, your first query is not executed, please try this:-
function addcontact()
{
if(isset($_POST['addContact']))
{
$officeName = strip_tags($_POST['office_name']);
$contactName = strip_tags($_POST['contactName']);
$contactNo = strip_tags($_POST['contactNo']);
$digitalNo = strip_tags($_POST['digitalNo']);
$mobileNo = strip_tags($_POST['mobileNo']);
$check= mysql_query("SELECT * FROM contacts WHERE office_name = '{$officeName}'");
if(mysql_num_rows($check) != 0)
{
echo "Already in Exists<br/>";
}else
{
$sql = mysql_query("INSERT INTO contacts (office_name, contact_no,
digital_no, mobile_no) VALUES
('$contactName','$contactNo','$digitalNo','$mobileNo')") or die(mysql_error());
if($sql)
{
header("Location: index.php?admin&done"); exit;
}
else
{
header("Location: index.php?admin&failed"); exit;
}
}
}
}
you can handle it from database side. write a stored procedure such a way that first check weather the record is in database or not if exist then ignore it and get back the text "Record already exist", if not exist then insert it to table. use conditional statements in mysql.