Emaziz Posted June 2, 2009 Share Posted June 2, 2009 Hi, My issue is that the MySQL script won't add any data to my database. I know the script works if it's not used in echo "script"; so I really don't know where the wrong is. Here is the script: <?php if (isset($_POST['username']) and trim($_POST['username'])!='') { echo "<?php $con=mysql_connect('','',''); # Information removed $condb=mysql_select_db('', $con); # Information removed $name='$_POST[username]'; $password='$_POST[password]'; $sendinfo=mysql_query('INSERT INTO `members`(`User`,`Pass`) VALUES (\'$name\', \'$password\')') or die('request - ' . mysql_error());?> <h1>Takk. Du kan nå logge inn </h1>"; } else { echo "<h2>Registrer deg her: </h2> <form action='#' method='post'> <li><label>Brukernavn:</label> <input tabindex='1' type='text' name='username' value='' maxlength='32' /><br /><br /></li> <li><label>Passord: </label> <input tabindex='2' type='password' name='password' maxlength='32' /><br /><br /> <input type='submit' value='Send inn'/></li> </form>";} ?> Any help is greatly appreciated. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/160664-solved-running-mysql-script-in-echo/ Share on other sites More sharing options...
Maq Posted June 2, 2009 Share Posted June 2, 2009 You don't echo PHP. You only echo out HTML if it's within <?php tags. In your case, it would be easier to use a closing ?> tag, and display HTML normally. Quote Link to comment https://forums.phpfreaks.com/topic/160664-solved-running-mysql-script-in-echo/#findComment-847883 Share on other sites More sharing options...
Emaziz Posted June 2, 2009 Author Share Posted June 2, 2009 Don't my problems just have the simplest solutions? Thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/160664-solved-running-mysql-script-in-echo/#findComment-847889 Share on other sites More sharing options...
Emaziz Posted June 2, 2009 Author Share Posted June 2, 2009 Don't my problems just have the simplest solutions? Thanks alot! It seems I cannot edit? Well. I figured out the problem isn't solved after all! Now it will store the data as "$name" and "$password" instead of "Mr.Example" and "Example123". Any idea why? Current code is: <?php if (isset($_POST['username']) and trim($_POST['username'])!='') { $ip=$_SERVER['REMOTE_ADDR']; $name=$_POST['username']; $password=$_POST['password']; $sendinfo=mysql_query('INSERT INTO `dummies`(`User`,`Pass`) VALUES (\'$name\', \'$password\')') or die('request - ' . mysql_error()); echo "<h1>Takk. Du kan nå logge inn </h1>"; }?> Quote Link to comment https://forums.phpfreaks.com/topic/160664-solved-running-mysql-script-in-echo/#findComment-847912 Share on other sites More sharing options...
Alex Posted June 2, 2009 Share Posted June 2, 2009 $sendinfo=mysql_query('INSERT INTO `dummies`(`User`,`Pass`) VALUES ($name, $password)') or die('request - ' . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/160664-solved-running-mysql-script-in-echo/#findComment-847915 Share on other sites More sharing options...
Maq Posted June 2, 2009 Share Posted June 2, 2009 That won't work. The variables won't interpolate because you wrap the string in single quotes. Try this: $sendinfo=mysql_query("INSERT INTO `dummies`(`User`,`Pass`) VALUES ('$name', '$password')") or die("request - " . mysql_error()); EDIT - You should also perform mysql_real_escape_string on your inputs to the database to prevent security issues. Quote Link to comment https://forums.phpfreaks.com/topic/160664-solved-running-mysql-script-in-echo/#findComment-847918 Share on other sites More sharing options...
sasa Posted June 2, 2009 Share Posted June 2, 2009 <?php if (isset($_POST['username']) and trim($_POST['username'])!='') { $ip=$_SERVER['REMOTE_ADDR']; $name=$_POST['username']; $password=$_POST['password']; $sendinfo=mysql_query("INSERT INTO `dummies`(`User`,`Pass`) VALUES ('$name', '$password')") or die('request - ' . mysql_error()); echo "<h1>Takk. Du kan nå logge inn </h1>"; }?> change ' to " in line where you create SQL string Quote Link to comment https://forums.phpfreaks.com/topic/160664-solved-running-mysql-script-in-echo/#findComment-847921 Share on other sites More sharing options...
Emaziz Posted June 2, 2009 Author Share Posted June 2, 2009 Yup that fixed it all'right! Thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/160664-solved-running-mysql-script-in-echo/#findComment-847922 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.