Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Code will not insert MySQL record (newbie question)

Status
Not open for further replies.

MikeMV

MIS
May 15, 2006
131
US
I am trying to learn php to manipulate MySQL data. I setup a simple input form and a script to insert a record to the database. I tried the code in a working web site which is hosted on GoDaddy and it works fine, but when I try it in my local practice machine it won't insert the record and I do not receive any errors. From what I have seen the process.php script never starts running I introduced an error on the first line, but I don't get the error, when I click on Process in the inputform.php script it gives me a blank page with the process.php name on the address back.

I wonder if you have any idea as to why process.php does not seem to run? The 2 scripts are:

inputform.php:

<form action="process.php" method="post">
Your ID: <input type="int(4)" name="id"><br>
Date: <input type="date" name = "day"><br>
Clock In: <input type="time" name = "clockin">
Clock Out: <input type="time" name = "clockout">
<br>
<input type="submit" value="Submit">
</form>

process.php:

<?
$id=$_POST['id'];
$day=$_POST['day'];
$clockin=$_POST['clockin'];
$clockout=$_POST['clockout'];
mysql_connect("localhost", "john", "Maintenance1") or die(mysql_error());
mysql_select_db("comptechdev") or die(mysql_error());
mysql_query("INSERT INTO clocks (id,day,clockin,clockout) VALUES ('$id', '$day', '$clockin','$clockout')");
Print "Your information has been successfully added to the database.";
?>

I am using Apache 2.22, MySQL 5.6.10, PHP 5.4.13 and phpMyAdmin 3.5.8. I can insert records manually using phpMyAdmin and all tests indicate that my setup is working properly. I setup the query.php as below and it does return results, I tried introducing an error in this query script by removing a semi-colon and I do get the error on the screen and see it in the log file, this seems to point out that the setup is working correctly.

<?php

// Make a MySQL Connection
mysql_connect("localhost", "john", "Maintenance1") or die(mysql_error());
mysql_select_db("comptechdev") or die(mysql_error());

// Retrieve all the data from the "clocks" table
$result = mysql_query("SELECT * FROM clocks")
or die(mysql_error());

// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry

echo "ID: ".$row['id'];
echo " Date: ".$row['day'];
echo " In: ".$row['clockin'];
echo " Out: ".$row['clockout'];

?>
I will greatly appreciate your response.
 
Just as an idea have you tried seeing if the insert is issuing an error?

Code:
mysql_query("INSERT INTO clocks (id,day,clockin,clockout) VALUES ('$id', '$day', '$clockin','$clockout')") or [b][COLOR=#A40000]die(mysql_error()[/color])[/b];



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
The insert does not seem to be the problem, I tried your code sample as follows and it does insert the record:

<?php

mysql_connect("localhost", "john", "Maintenance1") or die(mysql_error());
mysql_select_db("comptechdev") or die(mysql_error());
mysql_query("INSERT INTO clocks (id,day,clockin,clockout) VALUES ('235', '2013-04-15', '11:20:00','11:45:00')") or die(mysql_error());

?>

 
If you place a print or an echo at the top of your Process.php you don't see it?

If that's the case then it may not be running.

So I would ask how you are opening inputform.php. and where in relation to input form process.php is.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Remember to use MySQL_real_escape_string on all string and string like values being inserted to the database. Sprintf and similar used with array_map are useful tools for this
 
Phil.

The a print as the first line of process.php does not show up so it looks like process.php never runs

I open inputform.php by putting this on the address bar of FireFox localhost/inputform.php I tried this in Internet Explorer just in case it was a browser issue but the same thing happens.

I am not clear on the last question about where process.php is in relation to inputform.php, could you help me understand the question?

When I click on the submit button the address bar does change to and I don't get a "not found" error which seems to indicate the program understands that process.php needs to run.

Thanks for all your input.
 
The Address bar changes, but then nothing is displayed.... hmmmm

You are opening the file correctly so that's not the issue.

I am not clear on the last question about where process.php is in relation to inputform.php, could you help me understand the question?
I was just wondering if both fiels were in the same folder i.e Apache. But since you don't get a "Not Found" error it would seem it is finding it correctly.

Grabbing at straws here, the only other difference I can see between your insert and our process.php is the opening PHP tags.

Code:
[b][COLOR=#A40000]<?[/color][/b]
$id=$_POST['id'];
$day=$_POST['day'];
$clockin=$_POST['clockin'];
$clockout=$_POST['clockout'];
mysql_connect("localhost", "john", "Maintenance1") or die(mysql_error());
mysql_select_db("comptechdev") or die(mysql_error());
mysql_query("INSERT INTO clocks (id,day,clockin,clockout) VALUES ('$id', '$day', '$clockin','$clockout')");
Print "Your information has been successfully added to the database.";
?>

Try making it a fully qualified PHP opening tag:

Code:
<?php

Other than that, I'm at a loss as to what it could be.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Phil,

You are a genius! That's exactly what the problem was, once I added the php at the end of the line it worked.

Thanks so much for sticking with me and helping me resolve it. Hopefully I will have more intelligent questions as I go along. I am quite familiar with database work, but totally new at php.
 
Glad its working.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
why can't the insert query be failing? it's credible that the script is bailing at that point and either crashing the server (if the mysql library is corrupt eg) or just the script. if error display or reporting is turned off then nothing will be shown (although tbh i'd expect some feedback).

a good example of why the insert query might be failing could be

1. there is no escaping of inputs
2. there is a mismatch between column types and data input.

for the time being i'd try this. although I have to admit to being confused about why an ID is supplied AND an INSERT query is being used. i'd think that were an ID already existant, i'd be using an UPDATE ... WHERE query. and where an ID is not supplied I'd be setting the value to NULL in a query.

if this does not work then i'd like to see the create spec for the table.

Code:
<?php
echo "about to connect to db<br/>";
mysql_connect("localhost", "john", "Maintenance1") or die(mysql_error());
mysql_select_db("comptechdev") or die(mysql_error());
$fields = array('id','day','clockin','clockout');
foreach($fields as $field) $params[] = isset($_POST[$field]) ? mysql_real_escape_string($_POST[$field]) : '';
$query = vsprintf("INSERT INTO clocks (id,day,clockin,clockout) VALUES ('%s', '%s', '%s','%s')", $params);
echo "about to run this query $query<br/>";
mysql_query($query) or die(mysql_error());
echo "Your information has been successfully added to the database with the id: " . mysql_last_insert_ID();
?>
 
jpadie, mcampos says its working now.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
yes I see.

curious.

if the interpreter had not been firing due to the absence of the <?php I would have expected the browser to render the php code as a string, and thus leave it visible on the page.

perhaps it was treating it as xml and hiding the text as if it were source code.

in any event, my point on escaping the input is still hopefully taken on board by the OP
 
Yeah I find strange also. I'd second the idea that it was treating it as XML.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks jpadie,

I'll mess around with your sample code. I am totally new to php, I have done a lot of database work, but not with php.

I agree that the ID should come from another table, but this is just something I threw together to start playing around.
 
Before you go too far down this route remember that the mysql module has be depreciated infavour or mysqli or PDO
You should be able to get mysqli working with a simple search & replace although long term PDO may be a better apraoch. (that said I am still working on a legacy application that uses the mysql module)

A Maintenance contract is essential, not a Luxury.
Do things on the cheap & it will cost you dear
 
and although deprecated the non-scientific research that i have done seems to indicate that the mysql extension performs faster (on my machines) than mysqli or PDO (the latter is to be expected since it is an abstraction layer using - I believe - the mysqli library underneath).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top