0

The data got inserted into table successfully when I am inserting locally but it is not inserting when I am inserting on Server.

I have tried locally it got Inserted but When trying to add on Server it is not inserting nor giving any error!

if(isset($_REQUEST["submit_newlaw"]))
{
  $file=$_FILES["file"]["name"];

$court = $_POST['court'];
$citation = $_POST['citation'];
$appelant = $_POST['appelant'];
$opponent = $_POST['opponent'];
$judge = $_POST['judge'];
$judgement = $_POST['judgement'];
$reference = $_POST['reference'];
$year = $_POST['year'];
$detail = $_POST['detail'];

$tmp_name=$_FILES["file"]["tmp_name"];
$path="../case_laws/new/".$file;
$file1=explode(".",$file);
$ext=$file1[1];
$allowed=array("jpg","png","pdf","pdf","docx","doc");
if(in_array($ext,$allowed))
{
move_uploaded_file($tmp_name,$path);
$sql = "insert into new_caselaws(id, court, citation, appelant, opponent, 
judge, judgement, reference, year, detail,file)values('', '$court', 
'$citation', '$appelant', '$opponent', '$judge', '$judgement', 
'$reference', '$year', '$detail','$file')"; 
 mysqli_query($db, $sql);
 echo "Successfull";
 }

Expected result is to data got inserted successfully.

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • You do not do any error handling, so no wonder there is no error message. The dupkicate topic would show you how to handle mysql errors with mysqli. My guess is that your server does not allow empty string to initialise the auto increment field. You should use null or omit that field completely from the insert. – Shadow May 23 '19 at 00:19
  • It worked for me, Thank You so much brother. Your guess was 100% right --> "My guess is that your server does not allow empty string to initialise the auto increment field." – Sheeraz Ahmed May 26 '19 at 20:40
  • It was because I have not assigned any value to field "id", It was running fine Locally but giving error on Server-side. So, I removed "id" from insert Query because I have already selected it to Auto Increment from MySQL. My Query looks like this after removing id $sql = "insert into new_caselaws(court, citation, appelant, ...) – Sheeraz Ahmed Aug 07 '20 at 12:08

1 Answers1

-1

It seems like you have provided a link identifier returned by mysqli_connect() or mysqli_init() as the first parameter $db to your mysqli_query($db, $sql). however, it is not clear if you have defined the $db variable anywhere. If you have defined it, make sure you updated your mysqli_connect().

The following code might work on your local development machine, but the settings for connecting to your mysql server might be different. So make sure you update the values in the parameters.

mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

Also, it is really helpful to provide us with the error log.

If you cannot find your error_log, you can find it in the same directory as where this PHP code is located in.

For example, "public_html/error_log"

If your $db is actually defined somewhere and your mysqli_connect() is correct, you should make sure to write something like this instead around your mysqli_query() function

$qry = mysqli_query($db,$sql);
if($qry){
 echo "success";
} else {
 echo mysqli_error($db);
}

Lastly, make sure your file is getting uploaded in the server. If it is not uploaded, then it threw an exception and PHP never got to run your query because the

move_uploaded_file($tmp_name,$path);

failed and threw an exception ending your PHP script.

If you are not seeing php errors, go to your php.ini file and type this or make sure it is edited if already listed.

display_errors = On

After you edit or create this value in your php.ini file, which should be in your public_html or www directory, make sure you reset the web server to apply the settings.

I'am answering this question after it has been down voted because this could not necessarily be a mysql error since it is inserting properly in his local machine. Also, because some php.ini files might have error reporting off, in which case, it needs to be turned on to display errors.

  • Then add a link to one of the dozens of questions about handling php errors rather than providing the same answer again. Although I must point out if there was a php error, then the code would not have run locally either. – Shadow May 23 '19 at 00:51
  • Inserting and update done fine on the rest part of code, I don't know what issue actually occurred! By the way, Thank You so Much Sir. – Sheeraz Ahmed May 26 '19 at 03:10
  • It was because I have not assigned any value to field "id", It was running fine Locally but giving error on Server-side. So, I removed "id" from insert Query because I have already selected it to Auto Increment from MySQL. My Query looks like this after removing id $sql = "insert into new_caselaws(court, citation, appelant, ...) – Sheeraz Ahmed Aug 07 '20 at 12:09