Page 1 of 1
Newbie Question
Posted: Sat Mar 08, 2003 2:46 pm
by bobthebobert
I am currently trying to learn how to use MySQL through php, but I can't seem to get it. What is wrong with the following code?
examp.php
Code: Select all
<html>
<head></head>
<body>
<form action="examp2.php" method="POST">
<input type="text" name="user">
<input type="text" name="password">
<input type="submit" value="submit">
</form>
</body>
</html>
examp2.php
Code: Select all
<?php
$link = mysql_connect('localhost:3306', "$user", "$password")
or die("Could not connect: " . mysql_error());
print ("Connected successfully");
CREATE TABLE sup_table (mjgod VARCHAR(20)) TYPE=HEAP;
INSERT INTO sup_table (1,"$user", "$password");
?>
Posted: Sat Mar 08, 2003 6:56 pm
by evilcoder
There are a number of errors in the code, and its all in examp2.php
OK, now your form fields need to be given variable names. In PHP now, you should start using $_POST[''] to do so. Your examp2.php file should look like this now:
Code: Select all
<?php
$user = $_POST['user'];
$password = $_POST['password'];
$link = mysql_connect( "localhost:3306" , $user , $password )
or die( "Could not connect: " . mysql_error() );
print( "Connected successfully" );
CREATE TABLE sup_table (mjgod varchar(20) ) type=heap;
INSERT INTO sup_table (1 , '$user' , '$password' );
?>
But this still isn't right! Why? Because your not actually connecting to the database. Remove the $link variable so it becomes:
Code: Select all
<?php
$user = $_POST['user'];
$password = $_POST['password'];
mysql_connect( "localhost:3306" , $user , $password ) or die( "Could not connect: " . mysql_error() );
print( "Connected successfully" );
CREATE TABLE sup_table (mjgod varchar(20) ) type=heap;
INSERT INTO sup_table (1 , '$user' , '$password' );
?>
Your next problem is, the number of fields in your create table sql doesn't match the amount of fields your trying to add into in your INSERT query.
If you reading from a book i suggest burning it. hehehe
Posted: Sat Mar 08, 2003 7:25 pm
by hob_goblin
the $link variable can stay, and it's actually reccommended. it is a resource variable.
but what you need to do, is use post, and run the queries using mysql_query();
your code will end up like:
Code: Select all
<?php
$link = mysql_connect('localhost:3306', $_POSTї'user'], $_POSTї'password'])
or die("Could not connect: " . mysql_error());
print ("Connected successfully");
mysql_query("CREATE TABLE sup_table (mjgod VARCHAR(20)) TYPE=HEAP");
mysql_query("INSERT INTO sup_table (1,'$user', '$password')");
?>
Posted: Sat Mar 08, 2003 9:15 pm
by bobthebobert
Ahh, now I see.
I don't have a book, I am just trying to piece together some information.
Thanks for the help.
Posted: Sun Mar 09, 2003 10:36 am
by bobthebobert
Ok, now I have another problem. -.-
Code: Select all
$name = $_POSTї'name'];
$password = $_POSTї'name'];
$kingname = $_POSTї'kingname'];
$rulername = $_POSTї'rulername'];
$link = mysql_connect('localhost:3306', 'The Master', '')
or die('Could not connect: ' . mysql_error());
mysql_create_db(wwone);
mysql_select_db(wwone);
mysql_query("CREATE TABLE $name (mjgod VARCHAR(20)) TYPE=HEAP");
mysql_query("INSERT INTO $name (1,'$name','$password')");
mysql_query("INSERT INTO $name (2,'aa', 'cc')");
mysql_query("INSERT INTO $name (3,'bb', 'dd')");
For some reason, this lets me create a table, but it doesn't let me add anything into them.
Posted: Mon Mar 10, 2003 2:29 am
by twigletmac
Maybe a tutorial would be helpful?
http://www.phpcomplete.com
http://www.phpbuilder.com
http://www.devshed.com
You can also add or
die() statements after
mysql_query() and
mysql_select_db() calls to see how they are doing and
mysql_affected_rows() allows you to see if anything has been added:
Code: Select all
mysql_create_db('wwone');
mysql_select_db('wwone') or die(mysql_error());
$sql1 = "CREATE TABLE $name (mjgod VARCHAR(20)) TYPE=HEAP";
$sql2 = "INSERT INTO $name (1,'$name','$password')";
$sql3 = "INSERT INTO $name (2,'aa', 'cc')";
$sql4 = "INSERT INTO $name (3,'bb', 'dd')";
mysql_query($sql1) or die(mysql_error().'<p>'.$sql1.'</p>');
mysql_query($sql2) or die(mysql_error().'<p>'.$sql2.'</p>');
mysql_query($sql3) or die(mysql_error().'<p>'.$sql3.'</p>');
mysql_query($sql4) or die(mysql_error().'<p>'.$sql4.'</p>');
A likely issue is that you are trying to insert into 3 fields when your table only has one.
It may be useful to try
phpMyAdmin to see the syntax it uses to create a table containing the fields of the types you would like so you can replicate the code in your script. Also have a look at the information for
CREATE TABLE.
Mac
Posted: Mon Mar 10, 2003 3:32 pm
by bobthebobert
Gah sorry, I never got around to coming back and deleting my post. I finally found the mySql manual and realized how much of an idiot question that was. Sorry.