PDA

View Full Version : PHP A Beginers Guide "HELP"


mattemery
05-22-2007, 06:07 AM
DavidJ acn you help please?
I have been following your videos online for the above, but I am having problems with lesson 9, I have managed to get to the point where the data is input via the web page, but the data is not passed to the database, instead I get the error message that one field is not filled in.

Is it possible for me to download the source so that I can read it as it is very difficult to determine the type of ({[ that are being used (yes I am a novice), so I am guessing that this is why the code does not work.

Any help would be greatly received!! :lol:

(WIndows Vista Home Premium, EMS Manager Wamp5 and Dreamweaver CS)

Thank you
Matt Emery

davidj
05-22-2007, 07:54 AM
post the code you have currently

mattemery
05-23-2007, 06:00 AM
David, Code is as follows, thank you for your anticipated help: :wink:


<?php
require_once("connections/connection.php"); // database connection
//////////////////////////////////////
$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$submit = $_POST['submit'];
/////////////////////////////////////
if ($submit && $data && $data3 && $data4) {
/////////////////////////////////////
$query =sprintf("INSERT into table1 (field2, field3, field4) values ('data2', 'data3', 'data4')");
mysql_query($query)or die(mysql_error());
/////////////////////////////////////
}elseif($submit){
echo "One of your fields is empty, please complete all fields!";
}
/////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM table1");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
/////////////////////////////////////////////////////////////
//do{
//echo $row['field3'];
//}while ($row = mysql_fetch_array($result)

?>
<form name="form1" method="post" action="<?php $_{'PHP_SELF'}; ?>">
<table width="419" border="1" align="center">
<tr>
<td width="132"><div align="center">data2</div></td>
<td width="130"><div align="center">data3</div></td>
<td width="135"><div align="center">data4</div></td>
</tr>
<tr>
<td><label>
<div align="center">
<input type="text" name="data2" id="data2" />
</div>
</label></td>
<td><label>
<div align="center">
<input type="text" name="data3" id="data3" />
</div>
</label></td>
<td><label>
<div align="center">
<input type="text" name="data4" id="data4" />
</div>
</label></td>
</tr>
<tr>
<td colspan="3"><label>

<div align="center">
<input type="submit" name="Submit" id="Submit" value="Submit" />
<input name="submit" type="hidden" id="submit" value="1" />
</label>
</div></td>
</tr>
</table>
<div align="center"></div>
<p>&nbsp; </p>
</form>
<table width="424" height="56" border="1" align="center">
<tr>
<td height="23">ID1</td>
<td>data2</td>
<td>data3</td>
<td>data4</td>
</tr>

<?php do { ?>

<tr>
<td height="25"><?php echo $row['field1_id'];?></td>
<td><?php echo $row['field2'];?></td>
<td><?php echo $row['field3'];?></td>
<td><?php echo $row['field4'];?></td>
</tr>
<?php }while ($row = mysql_fetch_array($result)); ?>

</table>

davidj
05-23-2007, 08:21 AM
where you have this line


if ($submit && $data && $data3 && $data4) {


you have a variable $data but you need to name this $data2

if you look where you are setting these using $_POST's at the top of your page...


$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$submit = $_POST['submit'];


you can see you dont have one called $data so the IF conditional statement will fail.

mattemery
05-23-2007, 06:08 PM
David, thanks for that, could not see the wood for the trees.
Now it works to a point but when the data I enter is added it writes a new line to my db as:
data2 data3 data4
0 data3 0000-00-00

I entered 25 matt 2007-05-23

???????????????

davidj
05-23-2007, 08:18 PM
where you have...


$query =sprintf("INSERT into table1 (field2, field3, field4) values ('data2', 'data3', 'data4')");



you are using strings instead of variables where you have ... 'data2', 'data3', 'data4'

you need to check this stuff yourself as this is the only way you will learn. Check your syntax thoroughly.