Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tested your code and found that your submit button was one of the things at fault, including an improperly place semi-colon in: </p> <pre><code>$query = "INSERT INTO scoreboard (name,percent) VALUES ('$getname','$percentage');" </code></pre> <p>Which should read as:</p> <pre><code>$query = "INSERT INTO scoreboard (name,percent) VALUES ('$getname','$percentage')"; </code></pre> <h2>Tested using the following form and PHP</h2> <pre><code>&lt;form method="post" action=""&gt; &lt;center&gt; &lt;font color="green"&gt;Your name Max length is 15&lt;/font&gt; &lt;input type="text" name="username" maxlength="15"&gt; &lt;input type="submit" name="submit" value="Submit"&gt; &lt;/center&gt; &lt;/form&gt; &lt;?php if (isset($_POST['username'])) { $link = mysqli_connect("myhost","myusername","mypw","mydatabase") or die("Error " . mysqli_error($link)); // $getname = $_POST['username']; $getname = mysqli_real_escape_string($link,$_POST['username']); $percentage = "10"; $query = ("INSERT INTO scoreboard (name,percent) VALUES ('$getname',$percentage)"); $result = mysqli_query($link, $query); if(!$result){ printf("Error message: %s", mysqli_error($link)); } else { echo "Data properly inserted with the value of &lt;b&gt;$getname&lt;/b&gt; and &lt;b&gt;$percentage&lt;/b&gt;"; } } ?&gt; </code></pre> <p><strong>NOTE:</strong> You will be better off using the code below in order to check if the field is empty. Otherwise, clicking on the submit button without anything inside, will produce an entry in DB with a blank name field.</p> <pre><code>if (empty($_POST['username'])) { die("&lt;div align='center'&gt;Enter your name&lt;/div&gt;"); } else { // rest of code </code></pre> <p>Plus as stated by Hanky 웃 Panky, you should sanitize your variables like this, as done in my working example:</p> <pre><code>$getname = mysqli_real_escape_string($link,$_POST['username']); </code></pre> <hr> <p><strong>Here is a safer (parametrized) method</strong> as taken from an example on <a href="https://stackoverflow.com/a/60442/1415724">SO here</a> </p> <p><strong>Quick note:</strong> If you are going to use <code>$percentage = 10;</code> instead of <code>$percentage = "10";</code></p> <p>then you will need to use <code>$stmt-&gt;bind_param("si", $unsafe_variable,$percentage);</code> otherwise, your percentage will be treated as a string, as opposed to an integer and will be thrown an error. <code>s</code> is for <code>string</code> and <code>i</code> is for <code>integer</code>.</p> <pre><code>&lt;form method="post" action=""&gt; &lt;center&gt; &lt;font color="green"&gt;Your name Max length is 15&lt;/font&gt; &lt;input type="text" name="username" maxlength="15"&gt; &lt;input type="submit" name="submit" value="Submit"&gt; &lt;/center&gt; &lt;/form&gt; &lt;?php if (empty($_POST['username'])) { die("&lt;div align='center'&gt;Enter your name&lt;/div&gt;"); } else { $mysqli = new mysqli("myhost","myusername","mypw","mydatabase"); // Check that connection was successful. if($mysqli-&gt;connect_errno &gt; 0) { die('Connection failed [' . $mysqli-&gt;connect_error . ']'); } $percentage = "10"; $unsafe_variable = $_POST["username"]; $stmt = $mysqli-&gt;prepare("INSERT INTO scoreboard (name,percent) VALUES (?,?)"); // TODO check that $stmt creation succeeded // "s" means the database expects a string $stmt-&gt;bind_param("ss", $unsafe_variable,$percentage); $stmt-&gt;execute(); $stmt-&gt;close(); $mysqli-&gt;close(); echo "&lt;div align='center'&gt;Data written to DB&lt;/div&gt;"; } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload