I am encountering an issue with my PHP code that is causing an error

Whenever I try to submit my form, I keep encountering this error message. Can anyone offer some assistance with this issue?

Error:

Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or 
T_NUM_STRING in /home/u378761662/public_html/action.php on line 3

This is the PHP code causing the error:

<?php
$con=mysqli_connect("myhost.com","user","password","db");
$sql="INSERT INTO users values ($_POST["username"], $_POST["pwd"], $_POST["email"])";
if (mysqli_query($con,$sql)) {
echo "User added!";
}

This is the HTML form being used:

<form action="action.php" method="post">
  Username<input type="text" name="username">
  <p>
    Password<input type="password" name="pwd">
    <p>
      Email<input type="text" name="Email">
<p>
    <input type="submit" name="Sign Up" value="Sign Up">
</form>

Answer №1

One effective practice is to incorporate query parameters when dealing with dynamic values in SQL queries. This strategy minimizes confusion related to quotes and enhances security by preventing vulnerabilities.

It's a simple solution!

$sql="INSERT INTO users values (?, ?, ?)";
if ($stmt = mysqli_prepare($con,$sql)) {
  mysqli_stmt_bind_param($stmt, "sss", 
    $_POST["username"], $_POST["pwd"], $_POST["Email"]);
  if (mysqli_stmt_execute($stmt)) {
    echo "User added!";
  }
}

PS: You're Probably Storing Passwords Incorrectly

Answer №2

Dealing with a quoting issue:

$sql="INSERT INTO users values ($_POST['username'], $_POST['pwd'], $_POST['email'])";

The presence of the original "s led to parser errors. Additionally, as highlighted by @JohnConde, this leaves room for potential SQL injections.

Answer №3

Important Note: It is crucial to address the issue of vulnerability to SQL Injection. Explore Bill Karwin's suggestion for resolving this problem and safeguarding your system against such malicious attacks.

Focus on line 3 where the error lies - there seems to be an issue with the quoting:

$sql="INSERT INTO users values (" . $_POST["username"] . ", " . $_POST["pwd"] . ", " . $_POST["email"] . ")";

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing HTML5 Canvas for Shadow Effects with Gradients

Surprisingly, it seems that the canvas API does not support applying gradients to shadows in the way we expect: var grad = ctx.createLinearGradient(fromX, fromY, toX, toY); grad.addColorStop(0, "red"); grad.addColorStop(1, "blue"); ctx.strokeStyle = gra ...

Find and retrieve all data attributes with JavaScript

What is the method to retrieve all data-attributes and store them in an array? <ul data-cars="ford"> <li data-model="mustang"></li> <li data-color="blue"></li> <li data-doors="5"></li> </ul> The resultin ...

PHP - Implementing a Submit Button and Modal Pop-up AJAX across multiple browsers in PHP

As a newbie in PHP AJAX coding, I'm facing a challenge with having two browsers. My goal is to click the submit button on one browser and have a modal popup on the other browser simultaneously. Essentially creating a chat system where only a button an ...

Submitting via AJAX upon submission

I've implemented the following code in my comment.php: <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> <script type="text/javascript"> $(function() { $('#reset_form').cl ...

What is the best way to populate a Vue form with pre-existing data?

Currently, I am utilizing Vue form to edit a single item. However, I am struggling to determine the correct method for assigning values to categoryName and description in order to display those values within the form. I have attempted to retrieve the valu ...

Gather data from various HTML elements with JavaScript when submitting a form

How can I extract values from HTML elements and send them for processing through a form? I'm attempting to compile a menu item list with the individual items' structure in place, but I'm struggling to figure out how to fetch the values upon ...

Tips for aligning a select and select box when the position of the select has been modified

Recently, I encountered an interesting issue with the default html select element. When you click on the select, its position changes, but the box below it fails to adjust its position accordingly. https://i.stack.imgur.com/SwL3Q.gif Below is a basic cod ...

Combining Flask with npm: A Comprehensive Guide

I have a unique flask application that initiates with python3 app.py running on localhost:5000 Moreover, there is another distinct nodejs program that kicks off with npm run start which operates on localhost:3000 The challenge that lies ahead for m ...

Transforming JSP style tags into CSS declarations

Within my JSP file, I have various tags that look like this: <style type="text/css"> #mask { display: none; cursor: wait; z-index: 99999; position: absolute; top: 0; left: 0; height: 100%; ...

Is there a way to determine the selected radio button using Node.js?

I'm currently working on a form that includes two radio buttons. I am posting the request and using the body parser npm package to retrieve the values of the form inputs. Here is how the radio buttons are implemented in the form: <p> ...

Dependency on the selection of items in the Bootstrap dropdown menu

I am currently struggling with a few tasks regarding Bootstrap Dropdown Selection and despite looking for information, I couldn't find anything helpful. Check out my dropdown menu Here are the functions I would like to implement: 1) I want the subm ...

Example of creating a minimalistic Model-View-Controller (MVC) architecture

I am currently seeking a streamlined solution for a basic issue in php 5.3. Not facing any challenges with implementation, just contemplating the most efficient way to establish a neat resolution. This outlines my project: Data is requested from a publ ...

When attempting to save a base64 code to a directory, I encounter an issue where the image fails to load and cannot be accessed

I've been facing an issue where I try to save an image into a folder in my project, but the saved file appears corrupted and cannot be viewed or opened. Here's how I'm handling it: First, I receive the image through a form where I capture t ...

Jquery failing to function properly when scrolling

I have been attempting to implement a feature where an element changes its position to fixed after scrolling past a certain point. However, no change occurs when I scroll. In the <head> section of my code, I have included <script src="stiler/jquer ...

Which tag should I use, <b> or <mark>, to emphasize important marketing terms within a paragraph?

Trying to emphasize specific keywords in a paragraph of text can be challenging. I'm currently puzzled about the distinction between the <b> and <mark> tags. These words are highlighted because they play a crucial role in marketing, prompt ...

PHP is unable to show items containing special characters such as double quotes and apostrophes

I am facing an issue with ordering food items. Names that do not contain apostrophes work fine, but those like Pasqua Nero D'Avola Sicilia are causing problems. I have tried replacing ' with \', but the issue persists. Here is the relev ...

After clicking the submit button, how can I eliminate the following: "config.php?username=++psw&Text=psw&submit=send?" and remain on the same page?

Whenever I hit the submit button on my form, PHP completes its task but instead of staying on the page (or simply reloading it), I am redirected to the same URL with '/config.php?username=technologies.%0D%0A++&submit = Send ". Is there a way ...

Using jQuery to control mouseenter and mouseleave events to block child elements and magnify images

My objective is to create a hover effect for images within specific div elements. The images should enlarge when the user hovers their mouse over the respective div element. I plan to achieve this by adding a child element inside each div element. When the ...

Tips on appending a parameter to an image URL using JavaScript

On my website, I am able to insert images with specified width and height using this code: <img src="image.php?w=100&h=100"> I would like the image size to change based on the device screen width. For screens smaller than 600px, I want to displa ...

Using the "align" attribute is considered outdated and not recommended in HTML5 documents

I have encountered an error when using the align attribute. How can I fix this issue with the correct HTML5 compatible syntax? <table style="margin: 0 auto;"> <tbody> <tr> <td><input typ ...