0

My code is a mess right now, but I am just trying out some things. But I have the following pages:

index:

<html>

<head>
    <link rel="stylesheet" type="text/css" href="css\placing.css">
    <title>Numbers</title>
</head>

<body>
    <div class="topbar">
        sf
    </div>
    <div class="talraekke">
        <p>test</p>
    </div>
    <div class="content">
        <p>Enter The Number</p>
        <form action="action.php" method="post" >
            <input type="value" name="numbervalue">
            <input type="submit">
        </form>
    </div>

</body>
</html>

<?php 
include 'action.php';
?>

And my actionpage.php:

<?php

$username = "root";
$password = "root";
$hostname = "127.0.0.1:3306"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");
//echo "<br><br>Connected to MySQL<br><br>";

//select a database to work with
$selected = mysql_select_db("roulette_db",$dbhandle) 
  or die("Could not select any database");


echo "<h1>Hello " . $_POST["numbervalue"] . "</h1>";

    // Insert To Database
$strSQL = "INSERT INTO numbertable(numbers) VALUES('" . $_POST["numbervalue"] . "')";
// The SQL statement is executed 
    mysql_query($strSQL) or die (mysql_error());

    // SQL query
    $strSQL = "SELECT * FROM numbertable";

    // Execute the query (the recordset $rs contains the result)
    $rs = mysql_query($strSQL);

    // Loop the recordset $rs
    // Each row will be made into an array ($row) using mysql_fetch_array
    while($row = mysql_fetch_array($rs)) {

       // Write the value of the column FirstName (which is now in the array $row)
      echo $row['numbers'] . "<br />";
}

  // The SQL statement is executed 
    mysql_query($strSQL) or die (mysql_error());

    // Close the database connection
    mysql_close();
?>

From the database I recive some numbers that I have put in my form. But I would like that those numbers are inside the :

<div class="talraekke">
    <p>test</p>
</div>

But how can I call that data in my div tag?

Best Regards Mads

  • 2
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Dec 07 '15 at 19:11
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 07 '15 at 19:11
  • You would use AJAX to populate the div if you didn't want to reload the page. – Jay Blanchard Dec 07 '15 at 19:12
  • PHP cannot time travel. you don't run your db code until AFTER you've ended the html document. php cannot/will not reach back in time to retroactively insert your text into a different spot of the file. if you want your div to be populated, that's where you need to run your code. – Marc B Dec 07 '15 at 19:15
  • Hello. Thanks a lot for the answers. It is just for learning something about php and so on, and is never gonna be published anywhere. It is only running on my localhost. But I agree I will look to the PDO. So I had to make som AJAX for my div tag, to get the numbers inside my div, if I didnt want to reload? –  Dec 07 '15 at 19:20
  • "It is just for learning something about php" This is like learning to sail a boat by taking driving lessons. Working with code that has been unsupported for years will not teach you anything you want to know! – miken32 Dec 07 '15 at 21:53
  • But it will give me a background knowledge of the development of php... –  Dec 07 '15 at 23:16

1 Answers1

0

Just replace the below section from your code.

echo "<div class='talraekke'>";
while($row = mysql_fetch_array($rs)) {
    echo "<p>".$row['numbers'] . "</p><br />";
}
echo "</div>";
Vipin Singh
  • 543
  • 2
  • 8
  • 24
  • Why should the OP "try this"? A ***good answer*** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Dec 11 '15 at 13:41