Jump to content

[SOLVED] Need help connecting to a basic database with PHP5


rebeat

Recommended Posts

Hi,

 

I am new to php and mysql, so am going the php tutorial route. I found a nice tutorial but i think it is old and doesn't seem to be working with my PHP5 server.. ie: it is not submitting the data from the form to the database and incrementing.

 

The tutorial i am using is:

http://blazonry.com/scripting/linksdb/insert_data.php

 

Could someone please advise why the code is not working in PHP5/mysql5. I created the table (as shown) and used the register_globals vlues as they are off (ie: $anyvaluehere = $_POST['anyvaluehere']

 

My phpinfo file:

http://yournetworth.org/phpinfo.php

 

Hope someone can assist, i am only after a simple database i can add, edit values to so i can call them into a page dynamically.

 

Regards, Wayne.

 

HI, i used <?php in my code.

 

I don't get any error, i just submit the form then it returns a new form, nothing is added to the database, if you see the code, you will see there should be a confirmation note after successful addition of an entry... echo ("<P><B>New Link Added</B></P>\n");

 

but i don't get that, just a loop that never adds the entry.

you have to be a little more specific there man your just asking us to just guess your problem.

 

What kind of form are you using?

if its a registration form then have a look at diff registration tutorials instead else if you just want people to enter data into the db then do the following.

 

 

 

<?php 
//db information
$dbuser=""://db username
$dbpass=""://db password
$dbname="";//db name
dbhost="";// db host usually its localhost
//connect to db
$cid = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname, $cid);

//now check whether the user has clicked submit button. or not

if (isset($_POST['submit'])){// checks whther submit is clicked remember the input name and id of submit button should be submit value can be anything like "submit", "login" etc..

//now we place the data into the db
mysql_query("INSERT INTO tablename (FIELDname1, FIELDname2, FIELDname3) 
VALUES ($_POST['textfield1'], $_POST['textfield2'], $_POST['textfield3'])");
else{//if the submit button is not clicked display the form.

echo '<form methot="post" action="thenameofthispage.php">';
echo '<input type="text" name"textfield1" id="textfield1" />';
echo '<input type="text" name"textfield2" id="textfield2" />';
echo '<input type="text" name"textfield3" id="textfield3" />';
echo '<input type="submit" name"submit" id="submit" value="LOG IN" />';
echo '</form>';
}//end else
}//end if

?>

 

remember this is a simple script and its not secure :D but i don't want to confuse you just yet with security or other things it might be too much for you... take a look at the links here they might be useful for a begginer lool :D take care hope it helped you ciao.

 

of yeah dont forget to fillin db information lool because it will not work obviously

 

http://www.w3schools.com/php/php_forms.asp

http://www.phpfreaks.com/forums/index.php/topic,136739.0.html

Ok, a few things. Firstly, that code is pretty terrible. I would suggest this page as a series of much better tutorials.

 

Now, to your problems. This line...

 

if ($REQUEST_METHOD=="POST") {

 

Needs to be changed. The $REQUEST_METHOD variable has long been depricated and is probably disabled in a php5 statndard install. Try...

 

if (isset($_POST['submit'])) {

 

However, in order for this to work you will also need to add the name attribute to your forms submit button. eg;

 

<input type="submit" name="submit" value="Add Link">

 

Also note that all CAPS in html is not recommended and will not validate in xhtml.

thanks valtido and thorpev, the line if (isset($_POST['submit'])) { did the trick.

 

valtido, i wasn't asking people to guess the problem, as i stated, the code i used was "not submitting the data from the form to the database" its all ok now, you code thank you.

 

As for security, i found some code, but how do i make this a single php code like the one valtido created?

 

<?php

// Define your username and password
$username = "someuser";
$password = "somepassword";

if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {

?>

<h1>Login</h1>

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><label for="txtUsername">Username:</label>
    <br /><input type="text" title="Enter your Username" name="txtUsername" /></p>

    <p><label for="txtpassword">Password:</label>
    <br /><input type="password" title="Enter your password" name="txtPassword" /></p>

    <p><input type="submit" name="Submit" value="Login" /></p>

</form>

<?php
}
else {
?>

<p>This is the protected page. Your private content goes here.</p>

<?php
}
?> 

 

 

Thank you :)

i have another issue regarding the same database, i wish to edit the data, i have two files:

 

http://agtrac.com.au/test1/manage.php (to select the entry to edit)

http://agtrac.com.au/test1/manageedit.php (should display the current values)

 

I am using PHP5, my problem is, the form is blank but should echo the database values.

database table name is "products"

 

Source for manage.php

<?php
    $usr = "usr";
    $pwd = "pass";
    $db = "db";
    $host = "localhost";

    $cid = mysql_connect($host,$usr,$pwd);
    if (!$cid) { echo("ERROR: " . mysql_error() . "\n");    }

    if ($task=="del") {

        $SQL = " DELETE FROM products ";
        $SQL = $SQL . " WHERE id = $id ";
        mysql_db_query($db, $SQL, $cid);
    }

?>
<HTML>
<HEAD>
  <TITLE>Manage Links</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" >
<H1>Edit Links</H1>
<?php

    $SQL = " SELECT * FROM products ";
    $retid = mysql_db_query($db, $SQL, $cid);
    if (!$retid) { echo( mysql_error()); }
    else {
        echo ("<P><TABLE CELLPADDING=4>\n");
        while ($row = mysql_fetch_array($retid)) {
            $title = $row["title"];
            $id = $row["id"];

            echo ("<TR>");
            echo ("<TD>$title</TD>\n");
            echo ("<TD><A HREF=\"manageedit.php?id=$id\">Edit</A></TD>");
            echo ("<TD><A HREF=\"manage.php?id=$id&task=del\">Delete</A></TD>");
            echo ("</TR>");
        }
        echo ("</TABLE>");
    }
?>

</BODY>
</HTML>

 

Source for manageedit.php

<?php
    $usr = "user";
    $pwd = "pass";
    $db = "dbname";
    $host = "localhost";

    #connect to database
    $cid = mysql_connect($host,$usr,$pwd);
    if (!$cid) { echo("ERROR: " . mysql_error() . "\n");    }
?>
<HTML>
<HEAD>
   <TITLE>Update Link</TITLE>

</HEAD>
<BODY BGCOLOR="#FFFFFF">
<TABLE WIDTH="100%">
<TR><TD><FONT SIZE=5><B> Update Link </B> </FONT></TD>
     <TD VALIGN=TOP ALIGN=RIGHT><FONT SIZE="-1"><A HREF="javascript: self.close()">Close Window</A></FONT></TD>
</TR></TABLE>
<?

    # processed when form is submitted back onto itself
    if (isset($_POST['submit'])) {

        # setup SQL statement
        $SQL = " UPDATE products SET";
        $SQL = $SQL . " title = '$title', ";
        $SQL = $SQL . " description = '$description', ";
        $SQL = $SQL . " retail = '$retail', ";
        $SQL = $SQL . " special = '$special' ";
$SQL = $SQL . " special2 = '$special2' ";
        $SQL = $SQL . " WHERE id = $id ";

        # execute SQL statement
        $result = mysql_db_query($db,"$SQL",$cid);

        # check for errors
        if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");    }

        echo ("<P><B> Link Updated</B></P>\n");

    }
    else { # display edit form (not post method)

        # setup SQL statement to retrieve link
        # that we want to edit
        $SQL = " SELECT * FROM products ";
        $SQL = $SQL . " WHERE id = $id ";

        # execute SQL statement
	$ret = mysql_select_db($db,$SQL,$cid);

        # retrieve values
        $row = mysql_fetch_array($ret);
        $title = $row["title"];
        $description = $row["description"];
        $retail = $row["retail"];
        $special = $row["special"];
$special2 = $row["special2"];

?>

<FORM NAME="fa" ACTION="manageedit.php" METHOD="POST">
<INPUT TYPE="hidden" NAME="id" VALUE="<?php echo("$id"); ?>">
<TABLE>
<TR><TD><B>Title: </B> </TD><TD><INPUT TYPE="text" NAME="category" VALUE="<?php echo("$title"); ?>" SIZE=70></TD></TR>
<TR><TD><B>Description:</B> </TD><TD><TEXTAREA NAME="description" ROWS=5 COLS=40><?php echo("$description"); ?></TEXTAREA></TD></TR>
<TR><TD><B>Retail: </B> </TD><TD><INPUT TYPE="text" NAME="siteurl" VALUE="<?php echo("$retail"); ?>" SIZE=40></TD></TR>
<TR><TD><B>Special: </B> </TD><TD><INPUT TYPE="text" NAME="siteurl" VALUE="<?php echo("$special"); ?>" SIZE=40></TD></TR>
<TR><TD><B>Special2: </B> </TD><TD><INPUT TYPE="text" NAME="siteurl" VALUE="<?php echo("$special2"); ?>" SIZE=40></TD></TR>
<TR><TH COLSPAN=2><P><INPUT TYPE="submit" name="submit" VALUE="Update Link"></P></TH></TR>
</TABLE>
</FORM>


<?php    }

mysql_close($cid);

?>
</BODY>
</HTML>

 

I would also like to combine the two pages into one if possible (add/edit and display all) but this isn't as important as getting the form to display the values from the database.

 

 

Cheers.

 

i forgot to add, the delete link in "<a href=http://agtrac.com.au/test1/manage.php>manage.php</a> doesn't actually delete and once i click on an entry to jump to "<a href=http://agtrac.com.au/test1/manageedit.php>manageedit.php</a>" the "update" link returns an error. Feel free to use the submit button, it is all for testing purposes. the user/pass combinations i am using are all correct, i am able to add links <a href=http://agtrac.com.au/test1/insert_link.php>here</a>.  Regards.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.