Newbie Question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
bobthebobert
Forum Commoner
Posts: 25
Joined: Sat Feb 15, 2003 5:56 pm

Newbie Question

Post 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");
?>
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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&#1111;'user'], $_POST&#1111;'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')"); 
?>
bobthebobert
Forum Commoner
Posts: 25
Joined: Sat Feb 15, 2003 5:56 pm

Post 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.
bobthebobert
Forum Commoner
Posts: 25
Joined: Sat Feb 15, 2003 5:56 pm

Post by bobthebobert »

Ok, now I have another problem. -.-

Code: Select all

$name = $_POST&#1111;'name'];
$password = $_POST&#1111;'name'];
$kingname = $_POST&#1111;'kingname'];
$rulername = $_POST&#1111;'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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
bobthebobert
Forum Commoner
Posts: 25
Joined: Sat Feb 15, 2003 5:56 pm

Post 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.
Post Reply