How to return a success/failure message after running the query and how to check for exist data in product_code data array before saving the data?
$query = mysql_query("INSERT INTO `produk2`
(product_code,product_name,product_desc,product_type,product_price,product_img,product_img_name)
VALUES ('$kod','$namaproduk','$spesifikasi','$jenis','$harga','$image','$name')");
}
I tried to run this code and doesn't seems to work. Any ideas? I've gone wrong?
$result=mysql_query($query);
if($result)
{
echo "<br/>Data uploaded.";
}
else
{
echo "<br/>Data not uploaded.";
}
You should use mysql_error() or mysql_errno().
See the manual
manual
Add if else condition to the return result of query.
$query = mysql_query("INSERT INTO `produk2`
(product_code,product_name,product_desc,product_type,product_price,product_img,product_img_name)
VALUES ('$kod','$namaproduk','$spesifikasi','$jenis','$harga','$image','$name')");
if($query)
{
echo "Data inserted";
}
else
{
echo "Sorry! Data not inserted";
}
Related
updated code and it's now displaying success but still no image in my database.
anyone have any idea why the image isnt being inserted?
<?php
error_reporting( E_ALL );
?>
<?php
if (isset($_POST['submit'])) {
if (getimagesize($_FILES['Image']['tmp_name'])==FALSE) {
echo "failed";
} else {
$name=addslashes($_FILES['Image']['name']);
$image=base64_encode(file_get_contents(addslashes($_FILES['Image']['tmp_name'])));
saveimage($name,$image);
}
} else {
echo "error";
}
function saveimage($name,$image) {
$con = mysqli_connect('', '','', '');
$sql = "INSERT INTO items ('image', 'Description') VALUES ($name, $image)";
$query=!mysqli_query($con,$sql);
if ($query) {
echo "success";
} else {
echo "Not uploaded";
}
}
?>
I have Found the issue
Please update your query using i have given
$sql = "INSERT INTO items (image, Description) VALUES ('".$name."', '".$image."')";
You should check you image column datatype in database table this should be text or blob type.
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));
For some reason I keep getting the output failed i did die() right after my $query and got the desired result but after that nothing seems to work. Can somebody point out as to what I am doing wrong?
$query="UPDATE `u313495632_test`.`users` SET `firstname='$firstname',`surname`='$surname',`gender`='$gender' WHERE `users`.`id`='$user'";
if ($query_run = mysql_query($query)) {
echo 'Profile Updated';
} else {
echo 'Failed';
}
Please try the following code:
$query="UPDATE `u313495632_test`.`users` SET `firstname`='$firstname',`surname`='$surname',`gender`='$gender' WHERE `users`.`id`='$user'";
$query_run = mysql_query($query);
if (!$query_run) {
echo 'Failed';
} else {
echo 'Profile Updated';
}
And You should use mysqli or PDO. Mysql is deprecated.
`firstname= should be `firstname`= You forgot a back tick after the field name.
For example:
$qrInsert = "INSERT INTO DBASE1.DBO.TABLE1 VALUES ('sampVal','sampVal','sampVal')";
odbc_exec($msCon,$qrInsert);
if( 'the query if successfully executed' ){
//then do this
//if not then
}else{
//then do this
}
Is there an easy way to know if it is successfully inserted, or in other cases, updated, and deleted succesfully?
Thanks
Try like
if(odbc_exec($msCon,$qrInsert))
{
echo 'Executed Successfully';
} else {
echo 'Error in execution';
}
odbc_exec only will return true if the query executed successfully,or else return false if it is not
if (odbc_exec($msCon,$qrInsert)){
// do this
}
else{
// do that
}
just replace your code with
$qrInsert = "INSERT INTO DBASE1.DBO.TABLE1 VALUES ('sampVal','sampVal','sampVal')";
if( odbc_exec($msCon,$qrInsert); )
{
//then do this
//if not then
}
else
{
//then do this
}
It Return 0 or 1 depends on failure or success of your query.You Can store result of "odbc_exec" in a variable & compare it in 'If','Else' condition.Benefit of storing in a variable is ,you can use it where ever you want .
i.e.
$query_result = odbc_exec($msCon,$qrInsert);
if($query_result)
echo 'Executed Successfully';
else
echo 'Execuion Error';
when the info is successfully inserted, it's displaying the error message and saying that it's a duplicate entry for a primary key...I can't figure out why!
<?
$email=$_POST['email'];
$pw=$_POST['pw'];
mysql_connect('***','***','***');
#mysql_select_db('***') or die('Unable to select database');
$query = "INSERT INTO test_table VALUES ('','$email','$pw')";
mysql_query($query) or die(mysql_error());
if(mysql_query($query))
{
echo 'success';
}
else
{
echo 'failure' .mysql_error();
}
mysql_close();
?>
You are executing the query twice: first, in mysql_query($query) or die(mysql_error()); and second, in if(mysql_query($query)). So the second time the query executes the record is already there and thus the insertion fails.
You are executing same query twice.
$query_result = mysql_query($query) or die(mysql_error());
if ($query_result) {
echo 'success';
} else {
echo 'failure' . mysql_error();
}
Write this way, hope it will work.
Just delete this code from your php script and it will be fine.
if(mysql_query($query))
{
echo 'success';
}
else
{
echo 'failure' .mysql_error();
}
You make it running error twice in a time. You can also use mysql_affected_rows() to make sure the data is executed in database server. Return a string type value.
<?
$email=$_POST['email'];
$pw=$_POST['pw'];
mysql_connect('***','***','***');
#mysql_select_db('***') or die('Unable to select database');
$query = "INSERT INTO test_table VALUES ('','$email','$pw')";
if(mysql_query($query))
{
echo 'Data executed : '.mysql_affected_rows();
}
else
{
echo 'failure' .mysql_error();
}
mysql_close();
?>
Good luck and let me know the result.
$email=$_POST['email'];
$pw=$_POST['pw'];
$alerts = array();
if (trim($_POST['email']) == '') {
$alerts[] = "<div class='alert alert-danger' role='alert'> Enter your Email! </div>"; }
if (trim($_POST['pw']) == '') {
$alerts[] = "<div class='alert alert-danger' role='alert'> Enter your PW! </div>"; }
if (!count($alerts)) {
$query = "INSERT INTO test_table (email, pw) VALUES ('".$email."', '".$pw."')";
mysqli_query($this->conn, $query) or die (mysqli_connect_error());
return ['success' => true];
} else {
return ['success' => false, 'alert_m' => implode($alerts)."<br>"];
}