473,396 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why does my 'database insert' not work in production?

97 64KB
I moved my web app from development (local machine) to production (web server) and the script I have that inserts to the database won't work.

This is the script:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ('includes/DbCon.php');
  3.  
  4. //Set directory etc for image upload
  5. $target_dir = "images/photo/";
  6. $target_file = $target_dir . basename($_FILES["image"]["name"]);
  7.  
  8. if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file))
  9. {
  10. echo '<script type="text/javascript">';
  11. echo 'alert("News Items Saved")';
  12. echo '</script>';
  13. } else {
  14. echo "Sorry, there was an error with your file.";
  15. }
  16.  
  17. // Variables
  18. if (isset($_POST['headline']))$headline = $_POST['headline'];
  19. if (isset($_POST['body']))$body = $_POST['body'];
  20. if (isset($_POST['image']))$image = $_POST['image'];
  21.  
  22. //Insert into Db
  23. if(isset($_POST['submit'])){
  24. $query = "INSERT INTO news (`headline`, `body`, `image`) 
  25. VALUES ('$headline', '$body', '".$_FILES['image']['name']."');";
  26. $result = $mysqli->query($query);
  27. echo '<script type="text/javascript">';
  28. echo 'document.location.href = "/admin/admin-news.php";';
  29. echo '</script>';
  30. }
  31. else
  32. {
  33. echo '<script type="text/javascript">';
  34. echo 'alert("The news item was not able to be created. Please try again."). mysql_error()';
  35. echo '</script>';
  36. }
  37. $mysqli->close();    
  38. ?>
  39.  
Could it be server settings?
Jun 19 '15 #1
19 1738
Dormilich
8,658 Expert Mod 8TB
it could be. you’ll need to start debugging.
Jun 20 '15 #2
computerfox
276 100+
And how should he "start debugging"?

@OP did it work in your test environment? Are the two environments the same? Do you have the same version of PHP and MySQL? Are the services running?
Jun 20 '15 #3
tdrsam
97 64KB
This is embarrassing.

I was connecting to the wrong database. So, yeah, fixed that problem.

However, although I'm getting the read from the database, I can't get the insert. I'm almost sure it should work. I even put a line to display an error message after the execution of the insert statement, but the browser goes straight past it. It's inserting the image to the directory it's supposed to, then displays the javascript alert, then goes on the databse insert, runs past that to the javascript redirect and goes to the page it's supposed to. So, everything seems to be working except for when I check the database and the values that were supposed to be inserted are not actually in there.

Can't figure it out.
Jun 22 '15 #4
tdrsam
97 64KB
I've been conversing with someone at the hosting company about this and they suggested a var_dump, which showed me that the values for first two variables are posting correctly, but there's no value (nor even a listing of the name) for the 'image' variable, which is a file upload.

Also, there's a record of the submit button in the var_dump. Is it supposed to do that? It returns a string of 16 characters, being 'Create News Item', which is the value given at the form, so it seems to be passing that value which I suspect could be a problem. I tried removing the value attribute from the submit button but that didn't change anything. I even tried changing the input type from input to button. Also nothing.

I also tested the delete part of the system I'm making and that won't interact with the Db either. I thought if anything would work it'd be that, so it seems like it might not be the way I've set out the scripts that interact with the database but possibly the forms I'm using or the file upload.
Jun 24 '15 #5
computerfox
276 100+
Normally, there should be no need to talk to the hosting company. They're usually there just for the services and not the consulting. I've been there too, they're not very helpful....

Anyway, I modified the code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  require('includes/DbCon.php');
  3.  
  4.  //Set directory etc for image upload
  5.  $target_dir = "http://bytes.com/images/photo/";
  6.  $target_file = $target_dir . basename($_FILES["image"]["name"]);
  7.  
  8.  //Insert into Db
  9.  if(isset($_POST['submit'])){
  10.  // Variables
  11.   $headline=$_POST['headline'];
  12.   $body=addslashes($_POST['body']);
  13.   $image_path="default.png";
  14.   if($_FILES['image']['name']!=""){
  15.    if(move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)){
  16.     $image_path=$target_file;
  17.    }
  18.    else {
  19.     ?><script>alert("There was an error uploading your image...");</script><?php
  20.    }
  21.   }
  22.   //Completely off!
  23.   //$query = "INSERT INTO news (`headline`, `body`, `image`)
  24.   //VALUES ('$headline', '$body', '".$_FILES['image']['name']."');";
  25.   //$result = $mysqli->query($query);
  26.   //echo '<script type="text/javascript">';
  27.   //echo 'document.location.href = "/admin/admin-news.php";';
  28.   //echo '</script>';
  29.   $query="insert into news (headline,body,image) values ('$headline','$body','$image_path')";
  30.   $succ=$mysqli->query($query);
  31.   if($succ){
  32.     print '<meta http-equiv="refresh" content="0; url=./admin/admin-news.php" />'; 
  33.     //Don't use JavaScript to redirect.  What if it's diabled?
  34.    //Also, I believe the path is wrong for the redirect.
  35.   }
  36.   else{
  37.    ?><script>alert("The news item was not able to be created.  Please try again...");</script><?php
  38.   }
  39.  } 
  40.  //$mysqli->close();  //Not needed
  41. ?>
  42.  
Besides the notes I left, please remember the number one rule for debugging->Clean up the code.

You might also want to review the PHP Bible:
http://php.net/manual/en/index.php

Another useful link:
http://devzone.zend.com/6/php-101-ph...lute-beginner/

Good luck!
Jun 24 '15 #6
tdrsam
97 64KB
Thanks for the help and especially for taking your time to rewrite that code for me. I wish I could say that the problem is solved, but unfortunately not. I used your code and got the same result. I even changed the 'if success - redirect' to a var_dump and got the same result too. This one really has me stumped.
Jun 24 '15 #7
computerfox
276 100+
Do me a favor, right before the insert, can you print out all the variables and put the results in this thread?

Also, now I really don't understand the hype, but the newer version of MySQL tends to be messy. Would it be possible to move back to the older version?

Also, please add this block after the if($succ):
Expand|Select|Wrap|Line Numbers
  1. else{
  2.  print $mysqli->error();
  3. }
  4.  
Old MySQL:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  require('includes/DbCon.php');
  3.  
  4.  $target_dir = "http://bytes.com/images/photo/";
  5.  $target_file = $target_dir . basename($_FILES["image"]["name"]);
  6.  
  7.  if(isset($_POST['submit'])){
  8.   $headline=$_POST['headline'];
  9.   $body=addslashes($_POST['body']);
  10.   $image_path="default.png";
  11.   if($_FILES['image']['name']!=""){
  12.    if(move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)){
  13.     $image_path=$target_file;
  14.    }
  15.    else {
  16.     ?><script>alert("There was an error uploading your image...");</script><?php
  17.    }
  18.   }
  19.   $query="insert into news (headline,body,image) values ('$headline','$body','$image_path')";
  20.   $succ=mysql_query($query);
  21.   if($succ){
  22.     print '<meta http-equiv="refresh" content="0; url=./admin/admin-news.php" />'; 
  23.   }
  24.   else{
  25.    ?><script>alert("The news item was not able to be created.  Please try again...");</script><?php
  26.   }
  27.  } 
  28. ?>
  29.  
The old version of MySQL assumes that the following connection is made in the connect file:
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  $user="";
  3.  $pass="";
  4.  $db_name="";
  5.  mysql_connect("localhost",$user,$pass);
  6.  mysql_select_db($db_name);
  7. ?>
  8.  
Jun 24 '15 #8
computerfox
276 100+
Wait a minute... I just noticed the target directory.....
Is that an example? That upload won't actually work. You need the internal path of the server, not the URL.
Jun 24 '15 #9
tdrsam
97 64KB
I didn't actually change the target directory at all. I checked if the imagers I'd uploaded had gone into the directory and they're there, so I left that line as is.

I printed all the variable and this is the result:

Test 3Test 1,2

I added the error message in the else statement but it printed the variables instead. I'm going to try the old MySQL version now.
Jun 24 '15 #10
tdrsam
97 64KB
Your not going to believe this;

I swapped to the old version of the MySQL statements and now the database interactions are processing. When I check what's in the database, everything I inserted just now are in there.

However, after I hit the submit button, I get an error saying that the statements are deprecated and that I have to use either mysqli or PDO instead.

Maybe I'll have to try writing it in PDO and see if that works.
Jun 24 '15 #11
computerfox
276 100+
I believe it. Like I keep saying, the new version of MySQL is terrible. If the old version now works, I would keep it that way. Are the deprivation messages posting to the front end? If not, just leave it.
Jun 24 '15 #12
tdrsam
97 64KB
The problem now is that the error messages ARE posting to the front end, and they're cancelling the redirect. I wonder if there's a way to force the redirect? Maybe with a timer or something?
Jun 24 '15 #13
Dormilich
8,658 Expert Mod 8TB
there’s a reason why the old mysql functions are deprecated. (implicit globals, no prepared statements, etc.)

personally I find PDO easier to handle than MySQLi and it’s definitely a huge improvement over the old mysql functions.
Jun 24 '15 #14
tdrsam
97 64KB
I just tried the PDO functions and they made a fair bit of sense and were pretty easy to write, but I don't think the server I'm using has it installed and it's probably not likely that I'll be able to get it installed.
Jun 24 '15 #15
Dormilich
8,658 Expert Mod 8TB
PDO is a core extension (since PHP 5.1). although it may not have the mysql driver installed by default.
Jun 24 '15 #16
tdrsam
97 64KB
Yeah, that was the error I was getting, the driver isn't installed. But, I got a reply from the hosting company. They gave me a workaround that removes the error message that says the functions are deprecated. I'm going to use that for the time being. It works well enough for now, but I'm not planning on making it permanent.
Jun 25 '15 #17
computerfox
276 100+
Just stick with the old version....
Why are you so eager to use the newer version? It's not like you'll be using the "built in features" or the advanced stuff just yet. I've been building for ten years without the need for those fancy stuff that obviously don't work anyway.
Jun 25 '15 #18
I also use PDO it's more easier than the traditional way as long as you understand the flow and since we are in fast-facing period. I think improvements is not bad apparently it helps.
Jun 25 '15 #19
computerfox
276 100+
Improvements as in functions and connections that don't work? Messy functions that if they're not built correctly won't do anything? Now i've never used PDO, but I've used both version of MySQL and find that the older version is much cleaner, stable, and it works in most environments.
Jun 25 '15 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: anon | last post by:
How *EXACTLY* does Smart Navigation work? (I would really really really like for this Smart Navigation to work in Mozilla.) The inner workings and the code is what I am looking for. Does it...
1
by: John | last post by:
Hi, Does Dotfuscator really work? I mean is there no way to decompile your program when it's done? Or does it just make it more difficult to work with the decompiled code? Also, is there...
5
by: Bill Willyerd | last post by:
I have been looking for some documentation that would support or reject my opinion on Production -vs- Development naming conventions. I believe that each environment should be housed on separate...
9
by: chandramohan.mani | last post by:
Does Event handlers work in netscape. If yes means can anyone please help me. <HTML><SCRIPT LANGUAGE="JScript"> function mouseclick() { alert("I was clicked on " +...
12
by: Frank Hauptlorenz | last post by:
Hello Out there! I have a DB2 V7.2 Database (Fix11) on Win 2000 Professional. It was before a NT 4 based Domain - now it is a Win 2000 Domain. The database server is a domain member. Now...
7
by: | last post by:
I am having trouble figuring out to call a database INSERT procedure from a simple submit form. It appears I should use the onclick event to trigger the procedure called BUT when I do this I...
4
by: titan nyquist | last post by:
Why does ToTitleCase not work if the case is already in upper case? I have to make my string lowercase, first, before I pass to to ToTitleCase to have it work. Titan
2
by: Rahul Babbar | last post by:
Hi, I wanted to know on which parameters does database capacity depend on? Is it only dependent on the tablespaces size that i have allocated? I am getting the following. C:\Program...
1
by: simon.elbaz | last post by:
Hi, i use the following map: std::map<Key, std::vector< std::string >, KeyCmp mSQLElem; The following code does not insert the newly created pair: std::vector<std::stringvcTemp;...
2
by: servo | last post by:
I am using the following code. Why it does not insert record in the MySQL Table through php script: <?php $dbhost='localhost'; $dbuser='root'; $dbpass='newpass'; $mysqldb='mysql'; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.