php and database
Posted by: Peter Farrell (---.ipt.aol.com)
Date: February 04, 2006 03:20PM

I am attempting to set up a 2 field form which submits data to a mySQL database, which in turn outputs this onto a webpage. A little bit of interactivity for the user. In ASP no probs...php is another matter! I'm not even getting it to write to the database.

Here is my code:

Input page:

<?
$username="nickyfarrell";
$password="qsw8ba";
$database="nickydb";

$name=$_POST['name'];
$event=$_POST['event'];
$comments=$_POST['comments'];


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"winking smiley;

$query = "INSERT INTO tableme VALUES ('','$name','$event','$comments')";
mysql_query($query);

mysql_close();
?>


<form action="insert_info.php" method="post" name="nickyform" id="nickyform">

<table width="410" border="0" cellpadding="2">
<tr>
<td nowrap="nowrap"><div align="right">Name :&nbsp;</div></td>
<td nowrap="nowrap"><input name="name" type="text" id="name" />
</td>
</tr>
<tr>
<td nowrap="nowrap"><div align="right">Event :&nbsp;</div></td>
<td nowrap="nowrap"><input name="event" type="text" id="event" />
</td>
</tr>
<tr>
<td valign="top" nowrap="nowrap"><div align="right">Comments :&nbsp;</div></td>
<td nowrap="nowrap"><textarea name="comments" cols="40" rows="5" id="comments"></textarea>
</td>
</tr>
<tr>
<td nowrap="nowrap">&nbsp;</td>
<td nowrap="nowrap"><div align="left">
<input type="submit" name="SUBMIT" value="Submit" />
</div></td>
</tr>
</table>
</form>

Output Page:

<?
$username="nickyfarrell";
$password="qsw8ba";
$database="nickydb";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"winking smiley;
$query="SELECT * FROM tableme";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$name=mysql_result($result,$i,"name"winking smiley;
$event=mysql_result($result,$i,"event"winking smiley;
$comments=mysql_result($result,$i,"comments"winking smiley;

echo "<b>Name: "$name </b><br>"Event: "$Event <br>"Comments:" $comments<br><hr>;

$i++;
}

?>


I'm really sorry to trouble you but it all seems to be fine. I've set up my priveledges on the database as they should be. I'm not getting any error messages...it just isn't outputting anything expect the HTML bits.

Thank you for your time,

Peter

Options: ReplyQuote
Re: php and database
Posted by: Michael (---.dip.t-dialin.net)
Date: February 04, 2006 03:34PM

Hi,

is "short_open_tag = on" in your php.ini? Is there a semicolon before "short_open_tag = on"?

If yes change it and remove the semicolon or try it with "<?php" instead of "<?"

Best regards
Michael

Options: ReplyQuote
Re: php and database
Posted by: Peter Farrell (---.ipt.aol.com)
Date: February 04, 2006 07:41PM

I've changed the <? tag to <?php, as well as the php.ini file to accept short tags as suggested. Thanks.

The script runs, but I keep getting a colon as output, no matter what I put into my form!

I'm more than a little confused as I see now reason why it's doing this!

Data is being inputted to the database (I know this as the ID column is incrementing), but when I view it there is no 'text' there.

All I'm really trying to do is allow a user to insert a few paragraphs of text into a div via a form. I can do it using ASP but no luck with PHP.

Thank you again.

Options: ReplyQuote
Re: php and database
Posted by: Michael (---.dip.t-dialin.net)
Date: February 05, 2006 08:26AM

Hi,

when short_open_tag = on you can use the <? tag.

Change mysql_connect(localhost,$username,$password);

into

$link = mysql_pconnet("localhost", $username, $password) or die ("Unable to..."winking smiley;

with "localhost" in quotation mark and remove

mysql_close();

and write the loop as follows:

while ($row = mysql_fetch_object($result)
{
echo "<b>Name: ".$row->name."</b><br>Event: ".$row->Event."<br>Comments:".$row->comments."<br><hr>";
}

When the variables in the database are described in lower case you have to use lower case in your syntax. The same for upper case.

Best regards
Michael

Options: ReplyQuote
Re: php and database
Posted by: Michael (---.dip.t-dialin.net)
Date: February 05, 2006 08:27AM

Sorry

$link = mysql_pconnect("... I forgot the second "c"

Options: ReplyQuote
Re: php and database
Posted by: Michael (---.dip.t-dialin.net)
Date: February 05, 2006 08:28AM

while ($row = mysql_fetch_object($result))

Options: ReplyQuote


Sorry, only registered users may post in this forum.