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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.