PDA

View Full Version : PHP Form = mySQL



Shibby-Shabs
03-09-2010, 12:04 AM
I was wondering if anybody knows how I can have the data that's get's put in a form to go into a database.

What type of form? View http://lab.jspace.netai.net
Note that nothing on that form gets logged because I don't know how. It's just using $_POST.

Trigs
03-09-2010, 03:48 AM
You can't view it (like View Source).

If you want, post a form and I'll create the handler.

Shibby-Shabs
03-09-2010, 08:48 AM
The code is below but I'm not sure if you understand what I mean, when you press submit I want it to go to index.php?page=registered like it is already but how do I make it so the data in the forms creates a table like.


Name
Email
Password


register.php (PHP-Includes = index.php):

<h3>Account Register</h3>

<form action="index.php?page=registered" method="post"><br>
Username: <input type="text" name="username" /><br>
Email: <input type="text" name="email1" /><br>
Email (Verify): <input type="text" name="email2" /><br>
Password: <input type="password" name="password" /><br>
Password (Verify): <input type="password" name="password2" /><br>
<input type="submit" value="Register" /><input type="reset" value="Reset" />
<br>



Registered.php:

Thank you for registering, <?php echo $_POST["username"]; ?>!<br>
<br>
A verification email has been sent to <b><?php echo $_POST["email2"]; ?></b> so that we can confirm the email you provided is valid!<br>

LMS16
03-09-2010, 08:51 AM
Use the insert command,

config.php:



<?php
define("dbhost", "localhost"); // Where the database is hosted
define("dbuser", "root"); // The database's user
define("dbpass", "password"); // The database's user's password
define("dbname", "database"); // The name of the database

mysql_connect(dbhost, dbuser, dbpass) or die (mysql_error());
mysql_select_db(dbname) or die (mysql_error());
?>


form.php:



<?php
require_once("config.php"); // MySQL connection details
// Functions
function clean($string){
$string = stripslashes($string);
$string = htmlentities($string);
$string = mysql_real_escape_string($string);

return $string;
}
// Check if form is submitted
if(isset($_POST['submit'])){
$username = clean($_POST['username']);
$email = clean($_POST['email']);
$message = clean($_POST['message']);

mysql_query("INSERT INTO `table` (`username`, `email`, `message`) VALUES ('' . $username . '', '' . $email . '', '' . $message . '')") or die (mysql_error());
}
?>


That a quick 1 I made up there and then so its untested and will need editting :)

If you want me to edit it to work with your post above, let me know :) but its worth having a go at getting it working by yourself

Hope it helps.

Lew.

Shibby-Shabs
03-09-2010, 09:21 AM
Thanks, I can't be bothered doing it through hosting so I'll try it on my other computer later via, xampp.

Apolva
03-09-2010, 01:11 PM
Just noticed Lew's insert query is a little wrong, here's a fix:


mysql_query("INSERT INTO `table` (`username`, `email`, `message`) VALUES ('{$username}', '{$email}', '{$message}');") or die (mysql_error());

LMS16
03-09-2010, 01:25 PM
Just noticed Lew's insert query is a little wrong, here's a fix:


mysql_query("INSERT INTO `table` (`username`, `email`, `message`) VALUES ('{$username}', '{$email}', '{$message}');") or die (mysql_error());


Thanks for the correction but to my knowledge, both work just as well as each other? :)

Lew.

Apolva
03-09-2010, 01:29 PM
Thanks for the correction but to my knowledge, both work just as well as each other? :)

Lew.

You used two single quotes as opposed to a single and double. :P

Shibby-Shabs
04-09-2010, 02:26 AM
I'm trying to teach myself PHP so I haven't got that far as I'm still creating the tables via php script and I created the database easily and now I want to know why my tables aren't being created.

Here's my code.


<?php
$con = mysql_connect("localhost","root","");
if ($con)
{
echo "-<u>Connected to mySQL!</u> <br />";
}
else
{
die('Could not connect: ' . mysql_error());
}

// Create table

mysql_select_db("jacksphp", $con);
if ($con)
{
echo "-<u>Database Connected<u>";
}
else
{
die('Error connecting to database: ' . mysql_error());
}
$sql = "CREATE TABLE Persons
(
Firstname varchar(15),
Username varchar(15),
Email varchar(15),
Password vachar(15),
)";

// Execute query
mysql_query($sql,$con);

mysql_close($con);
?>

Shibby-Shabs
04-09-2010, 07:13 AM
It drives me mad how I can't edit my posts. I'll try and get this merged but can somebody tell me what's wrong with these codes? Note: for the first one the ?> at the end was added in as the code continues on.



<?php
$con = mysql_connect($POST_["host"],$POST_["username"],$POST_["password"]);
if ($con)
{
echo "-<u>Connected to mySQL.</u> <br />";
}
else
{
die('Could not connect: ' . mysql_error());
}
?>

Note: $POST_ is coming from a form.

and



<?php

//Install Directory Existence Check
if file_exists("test.php");
{
echo "Please delete install directory after installation!";
}
else
{
die();
}
?>

Trigs
04-09-2010, 12:31 PM
$_POST not $POST_

Personally, I find it easier to create tables in myphpadmin.

Shibby-Shabs
04-09-2010, 02:15 PM
Yeah mistype, I had $POST_ in the actual code, I've got it working now so that it goes to my database and then on another page I select the table and view it on a table using HTML but now the problem is it will keep adding.

Name - Email - Question
Name - Email - Question
Name - Email - Question

Of course it being the actual details but I'm not sure how to have it so when you press a button it deletes the single name - email - question. I'm learning from W3schools and that wasn't very helpful for this, so far.

Trigs
04-09-2010, 03:07 PM
Use/Learn from Lew's code.

Shibby-Shabs
05-09-2010, 02:26 AM
I'll give it a shot, I'm trying to put xampp on my usb so I don't have to return to my faulty computer.

23:02
05-09-2010, 03:29 PM
<?php

define('HOST','localhost');
define('USERNAME','root');
define('PASSWORD','');

$con = mysql_connect(HOST,USERNAME,PASSWORD) or die(mysql_error());

?>


this is more simplier way to check whether if the connect acutally works

Neil
</div>

LMS16
06-09-2010, 07:52 AM
<?php

define('HOST','localhost');
define('USERNAME','root');
define('PASSWORD','');

$con = mysql_connect(HOST,USERNAME,PASSWORD) or die(mysql_error());

?>


this is more simplier way to check whether if the connect acutally works

Neil
</div>

I do believe I had that in my post a little earlier on :) Never the less, we all might as well contribute as when we were all learning php ect, we wanted as much help as possible :)

As a pose to learning from my code, its a good ideas as using functions within your code saves time, both coding and loading time (i think :S) lol

Lew.

Want to hide these adverts? Register an account for free!