Insert Into query does not work

Hi, I am quite new to web building. I have below code but my Insert query does not Insert anything in my database. It does not show any error and work well in local host.

    <?php
    $con=mysqli_connect('localhost','id5253788_njb','*********','id5253788_njb');
    mysqli_select_db($con,'id5253788_njb')or die("cannot select DB");

    if(mysqli_connect_errno($con)){
    	echo "Could not connect to the db:". mysqli_connect_error();
    	}
    ?>	
    <form action='' method='POST'>
    	<textarea name='replymsg'></textarea>
    	<button name='reply'>Reply</button>
    </form>

    <?php
    	if (isset($_POST['reply'])){
    		$bodymsg=$_POST['replymsg'];
    		$sql="INSERT INTO msg (tomsg, toname, frommsg, fromname, bodymsg) VALUES ('tomsg', 'toname', 'frommsg', 'fromname', '$bodymsg')";
    		$query=mysqli_query($con,$sql);
    	echo "Message Sent";	
    	}			
    mysqli_close($con);
    ?>
    </html>

Can anyone help me out?

Try changing the button tag to </> < input type=“submit” name=“reply”>

Why are you selecting your DB twice?

    <?php
    $con=mysqli_connect('localhost','id5253788_njb','*********','id5253788_njb');

    if(mysqli_connect_errno($con)){
    	echo "Could not connect to the db:". mysqli_connect_error();
    	}
    ?>	
    <form action='' method='POST'>
    	<textarea name='replymsg'></textarea>
    	<button name='reply'>Reply</button>
    </form>

    <?php
    	if (isset($_POST['reply'])){
    		$bodymsg=$_POST['replymsg'];
    		$sql="INSERT INTO msg (tomsg, toname, frommsg, fromname, bodymsg) VALUES ('tomsg', 'toname', 'frommsg', 'fromname', '$bodymsg')";
    		$query=mysqli_query($con,$sql)or die(mysqli_error($con));
    	echo "Message Sent";	
    	}			
    mysqli_close($con);
    ?>
    </html>

Oh, and, your script can easily be injected
Check

Thanks for solving the problem. Thanks once again for giving me a sound knowledge about preventing injection.

1 Like