Jump to content

[SOLVED] Running MySQL script in echo


Emaziz

Recommended Posts

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! :)

Link to comment
https://forums.phpfreaks.com/topic/160664-solved-running-mysql-script-in-echo/
Share on other sites

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>"; }?>

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.

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.