Jump to content

I might be a newbie...


TeaseM

Recommended Posts

Hi. If anyone would care to enlighten me I would be real happy. I just started to study php/mysql and found the turorial on w3schools.com.I follow the tutorial for learning purposes of course but here is my problem. Even if I copy/paste the code fromw3schools.com php tutorial I can't get thescript to create tables or columns my mysql database. I set up my wamp server using a book and double checked it with other tutorials onlineand it should work fine. I can't figure out why I can't get the php script to communicate withmysql. Any help, tips and tricks would be appriciated. As I mentioned in the topic I am new to this. I know HTML, CSS and to some extent Javascript. Please help. /T

Link to comment
Share on other sites

which scripts did you test? can you post the link? genraly you need to create database and tables from phpmyadmin and then use php to pull and push data inside it.if you have come up any code ,posting would be helpfull too.

Link to comment
Share on other sites

I used the one where you set up a HTML form and then use a php script to INSERT INTO the sql database. The problem is that my php wont communicate with my sql server. I also tried to getthe php to CREATE DATABASE (wich worked), CREATE TABLE (wich wont work).

Link to comment
Share on other sites

ok. what happened when you ran it? does it show any errors ? i can see the insert example does not have any debugging code. you can check mysql_query() return value that if it successfull or not. if failed echo the reason using mysql_error() $res=mysql_query("INSERT INTO Persons (FirstName, LastName, Age)VALUES ('Peter', 'Griffin',35)"); if(!$res)echo mysql_error(); http://php.net/mysql_error

Edited by birbal
Link to comment
Share on other sites

i updated my post above. did you see it?

Link to comment
Share on other sites

Yes. I tried it and it says I have a problem with my syntax and to chek the reference on my sql version... I don't see where the syntax would fail even when I check the reference though. I use MySQL 5.5.20. The syntax that is causing the problem is after the VALUES or SET statement.

Edited by TeaseM
Link to comment
Share on other sites

can you post your updated code?

Link to comment
Share on other sites

Hmmm. Tried it once again without my editing of the code more then database and table names and now I can't even get the php to connect to the server. This is getting wierd. I don't even get an sql_error message. $con = mysql_connect('localhost','MyUserName',''MyPassword);if (!$con);{die('could not connect:' .mysql_error());}

Edited by TeaseM
Link to comment
Share on other sites

you have to put your username and password there. probably it is username=root and password is blank.

Link to comment
Share on other sites

This is what I get when trying to post information using INSERT INTO and VALUES.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') VALUESI use the same syntax as in the tutorial and as far as I know it should work. Yes?This is the code I use, there is more to it but here is where the syntax problem comes in. $sql = "INSERT INTOusers(user_name, user_pass, user_email ,user_date,)VALUES('" . mysql_real_escape_string($_POST['user_name']) . "', '" . sha1($_POST['user_pass']) . "', '" . mysql_real_escape_string($_POST['user_email']) . "',NOW(),"; $result = mysql_query($sql); I'm using a form to retrieve the data from the user.

Edited by TeaseM
Link to comment
Share on other sites

have you echo'd the query ($sql)? Can you post what the output of your updated code for making the query looks like?

Link to comment
Share on other sites

Hi again all you helpful. Sorry for my late reply on my own topic. It really doesn't matter what I do in the code. I tried it several times and either I get the synax failure or it just doens't do anything to the database.I tried all of the things you tipped me about here and I also tried to re write the whole script again and still nothing happens to de DB.

Link to comment
Share on other sites

we need to see the final code.

Link to comment
Share on other sites

if query is not working let see why it is not working. print out the error of mysql if mysql_query() rfeturn false which means its failing.

$result = mysql_query($sql);if(!$result)echo mysql_error();

Link to comment
Share on other sites

That is part of the problem. I don't get an error message. Everything looks fine but nothing gets inserted to the Database. And When I try to get it to do anything after the query, say let me know that it worked i.e. echo '1 entry added'; I get this "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') VALUES"instead The code I use is the same as I've posted before in this topic.

Link to comment
Share on other sites

just show post us the lastest code. it's so much easier than guessing. It's just easier to post the code after any changes, there's no telling what subtle changes have been made that you don't consider significant. It sounds like you still have a problem with your quotes.

Link to comment
Share on other sites

Ok. Here one of the codes I can't get to work. It is mostly taken from a tutorial I found online and is solely for my educational purpose. <?phpsession_start();$server ='localhost';$username ='MYUSERNAME';$password ='MYPASSWORD';$database ='TEST'; if(!mysql_connect($server, $username, $password)){exit('Error: could not establish database connection');}if(!mysql_select_db($database)){exit('Error: could not select the database');} echo '<h3>Sign up</h3><br />'; if($_SERVER['REQUEST_METHOD'] != 'POST'){ //Form echo '<form method="post" action=""> Username: <input type="text" name="user_name" /><br /> Password: <input type="password" name="user_pass"><br />Password again: <input type="password" name="user_pass_check"><br />E-mail: <input type="email" name="user_email"><br /> <input type="submit" value="Add category" /> </form>';}else{ $errors = array(); if(isset($_POST['user_name'])){//the user name existsif(!ctype_alnum($_POST['user_name'])){$errors[] = 'The username can only contain letters and digits.';}if(strlen($_POST['user_name']) > 30){$errors[] = 'The username cannot be longer than 30 characters.';}}else{$errors[] = 'The username field must not be empty.';} if(isset($_POST['user_pass'])){if($_POST['user_pass'] != $_POST['user_pass_check']){$errors[] = 'The two passwords did not match.';}}else{$errors[] = 'The password field cannot be empty.';} if(!empty($errors)) {echo 'Uh-oh.. a couple of fields are not filled in correctly..<br /><br />';echo '<ul>';foreach($errors as $key => $value) {echo '<li>' . $value . '</li>'; }echo '</ul>';}else{ $sql = "INSERT INTOusers(user_name, user_pass, user_email ,user_date,);VALUES('" . mysql_real_escape_string($_POST['user_name']) . "', '" . sha1($_POST['user_pass']) . "', '" . mysql_real_escape_string($_POST['user_email']) . "',NOW())"; $result = mysql_query($sql);if(!$result){//something went wrong, display the errorecho 'Something went wrong while registering. Please try again later.<br />';echo mysql_error(); //debugging purposes, uncomment when needed}else{echo 'Succesfully registered.';}}} ?> Might be a bit hard to read here but anyhow. There it is.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...