1

we are working on a school project. We are trying to use some simple MySQL to store data from a single HTML text form into a MySQL database.

My HTML looks like this:

<form action="sql/tilmeld-sms.php" method="post">
        <h2>Tilmelding til SMS:</h2>
        <input type="text" name="sms">
        <input type="submit" value="Submit">
        </form>

Our SQL looks like this:

<?php
$connect = mysql_connect(“localhost”, “ODBC”, “”); if (!connect) { die('Connection Failed: ' . mysql_error()); { mysql_select_db(“database_name”, $connect);

$user_info = “INSERT INTO sms (sms) VALUES ('$_POST[sms]')”; if (!mysql_query($user_info, $connect)) { die('Error: ' . mysql_error()); }
echo “Your information was added to the database.”;
mysql_close($connect); 
?>

When we run this on our localhost, we get a syntax error on line 4. Which means there is something wrong with this line of code:

$user_info = “INSERT INTO sms (sms) VALUES ('$_POST[sms]')”; if (!mysql_query($user_info, $connect)) { die('Error: ' . mysql_error

We know this is very simple MySQL. But its the first time we use it, and the first time we try to store data from a HTML form into a mysql database.

Other info:

  • Databasename: projekt32
  • Databasehost: localhost
  • Username: ODBC (We read this is the general username for localhost on Windows)
  • Password: no password on localhost we read
  • Tablename: sms

EDIT: This is the error code we get:

Parse error: syntax error, unexpected 'INTO' (T_STRING) in C:\xampp\htdocs\projekt-3-2\sql\tilmeld-sms.php on line 4

EDIT2: This is a school project, and MySQL is part of this project. We wont pass without using it, so suggesting other things we can do than MySQL wont work but thanks anyway!

user3414678
  • 95
  • 1
  • 8
  • 5
    Are you really using "smart quotes" (`“` and `”`) instead of simple double quotes (`"`)? – Mark Baker Dec 09 '14 at 12:17
  • 1
    Also, please, please, please, stop using MySQL and start using MySQLi or PDO with prepared statements/bind variables.... you're just learning, so don't start learn bad habits, but learn to do it correctly right from the start – Mark Baker Dec 09 '14 at 12:18
  • This is a school project, and MySQL is part of this project and we cant pass without using MySQL. So we have to do it this way. – user3414678 Dec 09 '14 at 12:25
  • schools should be teaching you the right way to code, not the wrong way Perhaps your teacher could do with learning the right way to code as well – Mark Baker Dec 09 '14 at 12:31
  • Just for further information, the parse error is a PHP error, _not_ a MySQL error. The PHP interpreter is saying that it can't understand the code because it doesn't conform to correct PHP syntax. As pointed out by others, this is because PHP expects strings to be surrounded by straight double quotes (") not curvy double quotes (“). – daiscog Dec 09 '14 at 12:36
  • You are completely right about our teacher. The worst part is, our teacher is not able to do MySQL himself. But is "teaching" us MySQL. Thank you for explaining the PHP thing, i corrected the errors but things are not working out yet. :) – user3414678 Dec 09 '14 at 12:52

3 Answers3

2

You are using back ticks instead of double quotes/single quotes.

UPDATED:

$connect = mysql_connect('localhost', 'ODBC', '');
if (!connect) {
  die('Connection Failed: ' . mysql_error());
} // ALSO Use } instead of { here.
mysql_select_db('database_name', $connect);

ALSO,

$user_info = "INSERT INTO sms (sms) VALUES ('".$_POST['sms'] ."')";
Pupil
  • 23,834
  • 6
  • 44
  • 66
  • This is the correct answer, except the OP is using curved quotation marks, not back ticks (a back tick is `). (Sorry to be pedantic!) – daiscog Dec 09 '14 at 12:33
1

Try this

<label for="sms">SMS</label>
                    <input id="sms" type="text" name="sms" />

<input type="submit" name="add" id="add" value="Go">




$sms = kontroll_form_data_string($_POST['sms'], 100);

if ( isset( $_POST['add'] ) ) {
                $db = mysql_connect('localhost', 'ODBC', '');
                mysql_select_db("projekt32") or die(mysql_error());

                    $insert = "INSERT INTO sms(sms) VALUES('$sms')";
                    if(mysql_query($insert)) {
                      echo("Done");
                    }
}
frantsium
  • 180
  • 3
  • 19
  • Thank you very much, this almost works I think. But we do get this error, when we try to add data: Access denied for user ''@'localhost' to database 'projekt32' But as far i know, there is no users on localhost so im not sure what to do. – user3414678 Dec 09 '14 at 12:36
  • Can u try to create user for this table ? Go to this table in database, pick privileges and add user, then add this new user and pass in to `$db = mysql_connect('localhost', 'ODBC', '');` – frantsium Dec 09 '14 at 12:40
  • We made a user and gave that user all privileges. And something happened, because when we try to add data we get no errors. Actually we get nothing but a blank site when we submit the data. But the data is not added to the table, when we open the table with PHPmyadmin its empty. – user3414678 Dec 09 '14 at 12:49
  • `$sms = kontroll_form_data_string` Is one new row, try to change in database sms type VARCHAR(100) – frantsium Dec 09 '14 at 13:00
  • copy and add this html and add this $sms line, and then change sms type to varchar(100) – frantsium Dec 09 '14 at 13:02
  • Hi again, I am afraid I am still getting a complete blank page after i press the submit buttom. I have used your updated settings. We really appreciate your help on this, this MySQL is actually a big part of our project. :) – user3414678 Dec 09 '14 at 13:03
  • do you already did that ? (copy and add this html and add this $sms line, and then change sms type to varchar(100) ) – frantsium Dec 09 '14 at 13:04
  • If answer help you, u can mark this answer useful :) – frantsium Dec 09 '14 at 13:06
  • oh I dident see your latest answer. Where should I add $sms = kontroll_form_data_string? And I will the VARCHAR to 100 the current one is 20. I will post as soon I know more. Thanks again for your help. – user3414678 Dec 09 '14 at 13:06
  • add this $sms line after the sumbit button, u have a sms type varchar(20) ? – frantsium Dec 09 '14 at 13:07
  • We got it working! THANKS a lot for your help! You just saved us hours of work in our school project! THANK YOU from all 4 of us. – user3414678 Dec 09 '14 at 13:08
  • If u have sms type varchar(20) then add this `$sms = kontroll_form_data_string($_POST['sms'], 20);` – frantsium Dec 09 '14 at 13:08
  • I made your answer the answer to my question and upvoted your answer aswell.. Is there anything else I should do? And thanks again, this was really beginning to frustrate us! ;) – user3414678 Dec 09 '14 at 13:11
  • Thanks, Have fun and great coding ahead. ;) – frantsium Dec 09 '14 at 13:15
0

I think , you should try "mysql_real_escape_string", Concrete example of where mysql_real_escape_string fails and Prepared Statements are necessary

$connect = mysql_connect(“localhost”, “ODBC”, “”); if (!connect) { die('Connection Failed: ' . mysql_error()); { mysql_select_db(“database_name”, $connect);

$sms = mysql_real_escape_string($_POST[sms]);

$user_info = “INSERT INTO sms (sms) VALUES ('$sms')”; if (!mysql_query($user_info, $connect)) { die('Error: ' . mysql_error()); }
echo “Your information was added to the database.”;
mysql_close($connect); 

I hope this helps you.

Community
  • 1
  • 1
Pragnesh Karia
  • 509
  • 2
  • 6
  • 14