Why is my or die() function not working [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I am trying to create a products page in a admin panel, where the administrator can input their products. I am trying to add the product to the database and use a or die output if anything goes wrong. But it seems like every time I type in or die, I receive a error code before evening running the code (last part of code).
What is the reason for this? Please help.
<?php
// parse from data
if (isset($_POST['product_name'])) {
$pid = mysql_real_escape_string($_POST['thisID']);
$Product_Name = mysql_real_escape_string($_POST['Product_Name']);
$desc = mysql_real_escape_string($_POST['Product_Desc']);
// See if that product name is an identical match to another product in the system
$sql = mysql_query("UPDATE products SET product_name='$Product_Name'LIMIT 1");
$productMatch= mysql_mum_rows($sql);
if ($productMatch>0){
echo "Sorry you tried to place a duplicate product name";
exit();
}
//add products to database
$sql = mysql_query("INSERT INTO Product(Product_Name,Product_Desc,date_added)
VALUES('$Product_Name','$desc',now())") or die(mysql_error())
<?php
//blocks gravs
?>

You forgot to terminate(;) your code in die part and you open again a php code but you forgot to close the part last php code.so to correct it might be like this.
<?php
//..Some of your code here
$sql = mysql_query("INSERT INTO Product(Product_Name,Product_Desc,date_added)
VALUES('$Product_Name','$desc',now())") or die(mysql_error());
//blocks gravs
?>

Related

How I use if else condition in my get_file_contents which I grab from database [duplicate]

This question already has answers here:
check if url exists in php [duplicate]
(7 answers)
Closed 3 years ago.
I am displaying contents from the text file from database through file_get_contents.
$sql = "SELECT * FROM myContent";
$query = mysqli_query($db, $sql);
$row = mysqli_fetch_row($query);
$file = file_get_contents('mytext.txt');
echo $file;
Everything fine, but when my file is missing I get errors. How I put if-else condition in my php code when file is missed or not in place.
Thank you.
if(file_exists('mytext.txt')) {
$file = file_get_contents('mytext.txt');
echo $file;
}
Hope this helps ;)
if(!empty(file_get_contents('mytext.txt'))):
// Display file contents
else:
// Error handling
endif;

What is the proper way to find an 'internal server error 500' in php? [duplicate]

This question already has answers here:
How do I get PHP errors to display?
(27 answers)
Closed 5 years ago.
I am continuously having this internal server error to a very simple php code working with JSON. The values I am logging from the code is absolutely correct, yet when in action I am getting an internal server error 500 from this particular code. I had a similar code working previously. What am I doing wrong? or how should I proceed with debugging the error?
<?php
$var2 = $_POST['phn'];
$phone_received = json_decode($var2);
$adb = PearDatabase::getInstance();
$query1 = "SELECT addressid FROM address WHERE mobile = ?";
$leadID = $adb->pquery($query1, array($phone_received));
$row= $adb->num_rows($leadID);
if ($row != 0) {
$result = 'This number has already been used in the system.';
echo json_encode($result);
}else{
$result = 'Good to go!';
echo json_encode($result);
}
?>
Did you try to add true parameter to your json_decode call? I suppose the problem is with the $phone_received param. I also suggest you line by line debugging: try to retrun some dummy result after each line of code step by step so you can detect the place where the error occurs.

Something going wrong in mysqli_query [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I am newbie and learning php yet. I am trying to run some mysqli_query but its giving me some errors. My code is like below.
<?php
ob_start();
include("db.php");
$result = mysqli_query($conn,"SELECT * FROM contest");
while($row = $result->fetch_assoc()){
if($row['automated'] ==1){
echo 'Atomatic is enabled';
$result1 = mysqli_query($conn,"UPDATE `contest` SET automated =0"
$new = mysqli_query($conn,$result1);
echo 'Atomatic is Disabled';
}
else{
echo 'Atomatic is Disabled';
}
}
can somebody check and please suggest me whats wrong in this query ? its giving me error like Parse error: syntax error, unexpected '$new' and similar if I change it. Thanks
Close patenthesis.()..
$result1 = mysqli_query($conn,"UPDATE `contest` SET automated =0");//error was here

Android post values to MySQL with PHP [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I work on an Android project, in which I use AsyncTask to send nameValuePairs to webservice.
I use PHP code below to send data from application to database:
<?php
require_once('dbConnect.php');
$studentid = $_POST['name'];
$classid = $_POST['classid']
$studentid = $_POST['studentid'];
$start = $_POST['start'];
$end = $_POST['end'];
$startsig = $_POST['startsig'];
$endsig = $_POST['endsig'];
$sql = "insert into signature (studentid,classid,start,end,startsig,endsig) values ('$studentid','$classid','$start','$end','$startsig','$endsig')";
if(mysqli_query($con,$sql)){
echo 'success';
}
else{
echo 'failure';
}
mysqli_close($con);
?>
Values in dbConnect.php file are correct. Database schema looks like this.
I checked, application sends out data successfully, but when I try to access PHP file above from browser I get a HTTP 500 error.
What else should I check in this case?
There are two problems I noticed in your code you didn't put a semicolon after
$classid = $_POST['classid']
And also why are you assigning two different post values to same phpvariable $studentid = $_POST['name']; and $studentid = $_POST['studentid'];.Anyways $studentid will have the last assigned value $_POST['studentid']
Try debugging for more errors

Can't execute PHP Mysql query, doesn't even open the page [duplicate]

This question already has answers here:
How do I get PHP errors to display?
(27 answers)
Closed 6 years ago.
So, I have some bug in this code that will not execute the query, it just says The localhost page isn’t working - localhost is currently unable to handle this request. And that error shows only when there's some bug in the code, so it won't execute it. I don't know what's the problem here, as I tried to connect to database and that's all ok. Problem lies in lines underneath the connect.php.
if($_POST) {
include('connection.php');
$id_broj = $_POST['id_num'];
$password = $_POST['password'];
$query=mysqli_query("SELECT id,id_broj,ime,prezime FROM zaposleni");
$result=mysqli_query($conn,$query);
$count=mysqli_num_rows($result);
$row=mysqli_fetch_array($result);
echo 'Passed';
}
Need to write only query in $query Variable as:
if($_POST) {
include('connection.php');
$id_broj = $_POST['id_num'];
$password = $_POST['password'];
$query="SELECT id,id_broj,ime,prezime FROM zaposleni";
$result=mysqli_query($conn,$query);
$count=mysqli_num_rows($result);
$row=mysqli_fetch_array($result);
echo 'Passed';
}

Categories