Jump to content

Alright everyone, i'm just starting PHP and SQL... 

I want to have a textbox and a submit button to be stored in a Database in MySQL.

For some reason it's not working, can anyone take a look at the PHP and tell me if there's something wrong? 

Thanks

 

HTML: 

<!DOCTYPE html><html><body><center><form action="action_page.php" method="post">Target:<br><input type="text" name="target1" value=""><br><br><input type="submit" value="Submit"></form> </body></html>

PHP:

<?php$servername = "serverIP";$username = "root";$password = "mysqldbpassword";$dbname = "mysql";// Create connection$conn = new mysqli($servername, $username, $password, $dbname);// Check connectionif ($conn->connect_error) {    die("Connection failed: " . $conn->connect_error);} $sql = "INSERT INTO db ('name')$VALUES ('target1');if ($conn->query($sql) === TRUE) {    echo "New record created successfully";} else {    echo "Error: " . $sql . "<br>" . $conn->error;}$conn->close();?>
Edited by prolemur
code tags
Link to comment
https://linustechtips.com/topic/383986-whats-wrong-with-this-php/
Share on other sites

Link to post
Share on other sites

My Guess is your SQL statement. Change to something like

$sql = "INSERT INTO db ('name') VALUES ('target1')";

Not that 'VALUES' doesn't have a question mark before it. Also, make sure you are escaping correctly and that you are indeed inserting the right values. Your current code looks to be inserting the plain text "target1" rather than the post data from the form. So, instead consider doing something like

$sql = "INSERT INTO db ('name') VALUES ('" . $_POST['target1'] . "')";

Not that that exact code is stupidly unsecure and is for demonstration purposes only 

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to post
Share on other sites

queries sent through the mysqli driver don't need semicolons.

 

But there is no PHP-endline semicolon, or just lack of quote sign. 

$sql = "INSERT INTO db ('name')$VALUES ('target1');

What i ment was, there is needed double quote sign before existing semicolon, or a double quote + semicolon at the end of the line.

Link to post
Share on other sites

My Guess is your SQL statement. Change to something like

$sql = "INSERT INTO db ('name') VALUES ('target1')";

Not that 'VALUES' doesn't have a question mark before it. Also, make sure you are escaping correctly and that you are indeed inserting the right values. Your current code looks to be inserting the plain text "target1" rather than the post data from the form. So, instead consider doing something like

$sql = "INSERT INTO db ('name') VALUES ('" . $_POST['target1'] . "')";

Not that that exact code is stupidly unsecure and is for demonstration purposes only 

don't put qoutes around the table names

$sql = "INSERT INTO db (name, name1, name2) VALUES ('name', 'name1',name2 )";

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×