PHP: Add, retrieve and display data in a MySQL database table

admin

Administrator
Staff member
Hello all.

I am just wondering if this is possible in PHP,a nd if so how do I do it?

1. I want any user to be able to add an entry to a table in a MySQL database

2. I want any user to be able to view the content of a table in MySQL database.

I have an account at Lycos/Tripod UK, which DOES support PHP and I have 1 MySQL Database. I run a PHP forum successfully.

Cheers,for one, a lot can be learned if you read the php manual, and I have a few scipts on my site

here is one

<!-- m --><a class="postlink" href="http://www.snippetlibrary.com/viewhtml.php?id=6&kid=12&siteid=101Thanks">http://www.snippetlibrary.com/viewhtml. ... =101Thanks</a><!-- m --> Scoutt, I have found your site quite useful. I'm jusy wonderin, in that scipt you gave me, do I have to put in my username and password for the connection to the database. Also, am I correct in saying that the user will not be able to see the PHP script by pressinf View Source?

Thanksyes you have to insert you username and password and DB name.

no user (unless they hack into your system or Download your site with a website Download er) can see your php source. but it is suggested to keep the connection stuff in another file.Thanks, how do I go about including a link from my password file into my main PHP script?if you have your info for your Db named connect.php then include it like this

<?
// main page
include_once("connect.php");

//then the rest of the page normally.

?>Thank you for your help. Everything is working so far, however I have another question:

This is the PHP script I am using now, in one of my webpages.

<?
mysql_connect ("localhost","usrname","pswd");
mysql_select_db ("database");

$sql = "select * from mohdatabase";
$result = mysql_query ($sql);

while ($row = mysql_fetch_array($result))
{
$field1= $row["field 1"];
$field2= $row["field 2"];
$field3= $row["field 3"];
$field4= $row["field 4"];

echo "$field1<br>";
echo "$field2<br>";
echo "$field3<br>";
echo "$field4<p>";

}
?>

In the output, how do I make field 4 a link. Can I include:

echo "<a href=http://www.htmlforums.com/archive/index.php/"url.html">$field4</a><p>";

Would that work? Also, do you have a basic script that allows visitors to submit to a database?

Thank you so far!yes you can do that with the link, no problem.

a basic script for inserting into a DB, hmm you can't really get basic fo rthat as it depends on what version of php you are working with. not to hard though. just make a form of what you want to be intered into the DB and then send it the form to the same page as your insert script.

make the form and I will do the rest, bu tI have to know what the form variables are.Scoutt, please read attahced file for the info.

Thanks,here you go.

rename the attached to form.phpThanks Scoutt.

I'll have a play and see wht can do.

Thanks!Hi.

Everything is working fine, apart from the Submit form. The form displays great, but the submit does not work.

Here is the form.php file:

----------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>

<title>title</title>

</head><body bg color="ffffff">
<?
if ($_POST['send']){
include ("connect.php");

$sql_insert = mysql_query("insert into mohdatabase (Webmaster_Name, Webmaster_Email, Site_Name, Site_URL)
values (
'".addslashes(htmlspecialchars($_POST[Webmaster_Name]))."',// these lines protect you from people.
'".addslashes(htmlspecialchars($_POST[Webmaster_Email]))."', // entering malicious code
'".addslashes(htmlspecialchars($_POST[Site_Name]))."',
'".addslashes(htmlspecialchars($_POST[Site_URL]))."' )");
echo "Thank you for adding your site"; // have anyhting you want once the info has been inserted.
// also can forward to another page once this is done.
// just give a link here then.
exit;
}

?>

<form method="post" action="form.php">

<table cellpadding="0 cellspacing="0" align="center">
<tr><td align="right">Your full name:</td>
<td align="left"><input type="text" size="30" name="Webmaster_Name"></td></tr>

<tr><td align="right">Your e-mail address:</td>
<td align="left"><input type="text" size="30" name="Webmaster_Email"></td></tr>

<tr><td align="right">Name of your site:</td>
<td align="left"><input type="text" size="30" name="Site_Name"></td></tr>

<tr><td align="right">Site URL:</td>
<td align="left"><input type="text" size="30" name="Site_URL"></td></tr>

<tr><td> </td><td align="center">

<INPUT TYPE="submit" VALUE="Submit" name="send"></td></tr>
</form>
</table>

</body>
</html>

---------------

And the connect.php file:

---------------

connect.php

<?php
// variables used to open connection to the database
$server = "localhost";
$user = "comhub";
$pass = "password";
$db = "database";

// open connection
$connection = mysql_connect($server, $user, $pass);

// select database
mysql_select_db($db, $connection);

?>
--------------

Thank you,

Philyou might wanna try to change the $_POST['send'] to $_REQUEST['send'] instead. I have found $_POST to be a little flakey :)
 
Back
Top