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: -
<?php
-
include ('includes/DbCon.php');
-
-
//Set directory etc for image upload
-
$target_dir = "images/photo/";
-
$target_file = $target_dir . basename($_FILES["image"]["name"]);
-
-
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file))
-
{
-
echo '<script type="text/javascript">';
-
echo 'alert("News Items Saved")';
-
echo '</script>';
-
} else {
-
echo "Sorry, there was an error with your file.";
-
}
-
-
// Variables
-
if (isset($_POST['headline']))$headline = $_POST['headline'];
-
if (isset($_POST['body']))$body = $_POST['body'];
-
if (isset($_POST['image']))$image = $_POST['image'];
-
-
//Insert into Db
-
if(isset($_POST['submit'])){
-
$query = "INSERT INTO news (`headline`, `body`, `image`)
-
VALUES ('$headline', '$body', '".$_FILES['image']['name']."');";
-
$result = $mysqli->query($query);
-
echo '<script type="text/javascript">';
-
echo 'document.location.href = "/admin/admin-news.php";';
-
echo '</script>';
-
}
-
else
-
{
-
echo '<script type="text/javascript">';
-
echo 'alert("The news item was not able to be created. Please try again."). mysql_error()';
-
echo '</script>';
-
}
-
$mysqli->close();
-
?>
-
Could it be server settings?
19 1738
it could be. you’ll need to start debugging.
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?
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.
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.
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: -
<?php
-
require('includes/DbCon.php');
-
-
//Set directory etc for image upload
-
$target_dir = "http://bytes.com/images/photo/";
-
$target_file = $target_dir . basename($_FILES["image"]["name"]);
-
-
//Insert into Db
-
if(isset($_POST['submit'])){
-
// Variables
-
$headline=$_POST['headline'];
-
$body=addslashes($_POST['body']);
-
$image_path="default.png";
-
if($_FILES['image']['name']!=""){
-
if(move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)){
-
$image_path=$target_file;
-
}
-
else {
-
?><script>alert("There was an error uploading your image...");</script><?php
-
}
-
}
-
//Completely off!
-
//$query = "INSERT INTO news (`headline`, `body`, `image`)
-
//VALUES ('$headline', '$body', '".$_FILES['image']['name']."');";
-
//$result = $mysqli->query($query);
-
//echo '<script type="text/javascript">';
-
//echo 'document.location.href = "/admin/admin-news.php";';
-
//echo '</script>';
-
$query="insert into news (headline,body,image) values ('$headline','$body','$image_path')";
-
$succ=$mysqli->query($query);
-
if($succ){
-
print '<meta http-equiv="refresh" content="0; url=./admin/admin-news.php" />';
-
//Don't use JavaScript to redirect. What if it's diabled?
-
//Also, I believe the path is wrong for the redirect.
-
}
-
else{
-
?><script>alert("The news item was not able to be created. Please try again...");</script><?php
-
}
-
}
-
//$mysqli->close(); //Not needed
-
?>
-
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!
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.
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): -
else{
-
print $mysqli->error();
-
}
-
Old MySQL: -
<?php
-
require('includes/DbCon.php');
-
-
$target_dir = "http://bytes.com/images/photo/";
-
$target_file = $target_dir . basename($_FILES["image"]["name"]);
-
-
if(isset($_POST['submit'])){
-
$headline=$_POST['headline'];
-
$body=addslashes($_POST['body']);
-
$image_path="default.png";
-
if($_FILES['image']['name']!=""){
-
if(move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)){
-
$image_path=$target_file;
-
}
-
else {
-
?><script>alert("There was an error uploading your image...");</script><?php
-
}
-
}
-
$query="insert into news (headline,body,image) values ('$headline','$body','$image_path')";
-
$succ=mysql_query($query);
-
if($succ){
-
print '<meta http-equiv="refresh" content="0; url=./admin/admin-news.php" />';
-
}
-
else{
-
?><script>alert("The news item was not able to be created. Please try again...");</script><?php
-
}
-
}
-
?>
-
The old version of MySQL assumes that the following connection is made in the connect file: -
<?php
-
$user="";
-
$pass="";
-
$db_name="";
-
mysql_connect("localhost",$user,$pass);
-
mysql_select_db($db_name);
-
?>
-
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.
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.
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.
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.
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?
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.
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.
PDO is a core extension (since PHP 5.1). although it may not have the mysql driver installed by default.
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.
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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 " +...
|
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...
|
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...
|
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
|
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...
|
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;...
|
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';
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |