Jump to content

SQL SQLI


adrian369

Recommended Posts

<?php

        /* Connecting, selecting database */
        /* Please change the values below to connect to the users database...*/

        $mysqli_host="db749575m";
        $mysqli_user="********";
        $mysqli_password="*******";
        $my_database = "db72";
        $table_name = "nametable";
       
        echo "adder22";

        /*Please Do not change anything below this line */

        $link = mysqli_connect("$mysqli_host", "$mysqli_user", "$mysqli_password", "$my_database")
           or die("Could not connect : " . mysqli_error($link));
        echo "Connected successfully";
        mysqli_select_db($link, "$my_database") or die("Could not select database");


   
 

    the code above is working and connecting to the database.
 
code below original was in sql format and not sure how to upgrade it?
=================================================================================

  
$sqli="INSERT INTO nametable (firstname, lastname)

VALUES

('$_POST[fname]','$_POST[lname]')";



if (!mysqli_query($sqli,$con))

{

die('Error: ' . mysqli_error());  <<<< only getting this far recordiing error on screen>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

}

echo "1 record added";



mysqli_close($con)

?>




not sure how to fix this as hosts now wont let you send in support tickets. There SQL connect codes dont work.

Edited by Barand
Code tags added
Link to comment
Share on other sites

Oh - the suspense!!

 

So what IS this error on the screen?  Remember - we can't see your screen....

PS

This:

	  
$sqli="INSERT INTO nametable (firstname, lastname)

VALUES

('$_POST[fname]','$_POST[lname]')";
if (!mysqli_query($sqli,$con))

would be better this way  (indices need to be in quotes!):

	$q = 'insert into nametable (firstname, lastname)
	        values(' . "$_POST['fname']" .', ' . "$_POST['lname']" . ')';  
	if (!mysqli_query($q, $con))
	......
	
Edited by ginerjm
Link to comment
Share on other sites

15 hours ago, ginerjm said:

would be better this way  (indices need to be in quotes!):


	$q = 'insert into nametable (firstname, lastname)
	        values(' . "$_POST['fname']" .', ' . "$_POST['lname']" . ')';  
	if (!mysqli_query($q, $con))
	......
	

Just be aware that the POST variables would need to be enclosed in curly brackets with the above code.

$q = 'insert into nametable (firstname, lastname)
	        values(' . "{$_POST['fname']}" .', ' . "{$_POST['lname']}" . ')';

Or you could remove the double quotes

$q = 'insert into nametable (firstname, lastname)
	        values(' . $_POST['fname'] .', ' . $_POST['lname'] . ')';

In the end, however, it would be better to follow Barand's advice regarding prepared statements. That way you don't need to worry about the query breaking when someone with an apostrophe in their name shows up. Prepared statements will also protect you from SQL Injection attacks.

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.