RW8
RW8

Reputation: 31

Entering data into mysql database using php

Customer will complete a form and enter a pathway where they will want the CSV to be exported to. The pathway is entered using the top section of the php (below):

    <form action="register.php" method="post">
        Enter file pathway where CSV will be saved: <input type="text" name="username" required="required"/> <br/>
        <input type="submit" value="Enter"/>
    </form>
</body>

I want to create a variable called pathway. At the moment I can get text entered into the correct row in the mysql database (I can get John printed in the database), but not the correct text that was entered into the form (i.e. $pathway). I want to create a variable because after saving the pathway in the database i also want to use it in an export.php.

I am assuming i also need something like this:

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $pathway = mysql_real_escape_string($_POST['pathway']); 
// but i can't seem to piece it altogether.


    <?php
    $servername = "localhost";
    $username = "";
    $password = "";
    $dbname = "first_db";
    $table_users = $row['pathway'];
    $pathway = "pathway";                                             

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "INSERT INTO users (pathway)
    VALUES ('John')";

    if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
    } else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }

    mysqli_close($conn);
    ?>

Upvotes: 0

Views: 110

Answers (4)

Negom
Negom

Reputation: 318

This shoud work, if not then check your usename and password...

<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "first_db";
$pathway = $_POST['username'];  username - is the name of your input.                                             

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO users (pathway)
    VALUES ('$pathway')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

Upvotes: 1

pavon147
pavon147

Reputation: 713

First of all, you've got 'username' as the name of the field using for type a pathway, so rename it to 'pathway'. I don't know if I understand you, but do you just want to read posted content? Try something like:

$pathway = $_POST['pathway']

I strongly recommend to use object-oriented style with

$conn = new mysqli...

and then

mysqli->prepare(), mysqli->bind_param(), mysqli->execute()

With this you won't have to deal with mysqli_real_escape_string etc.

Upvotes: 0

Meenesh Jain
Meenesh Jain

Reputation: 2528

Your input field name is username

change it to pathway for $_POST['pathway'] to work

<form action="register.php" method="post">
    Enter file pathway where CSV will be saved: 
     <input type="text" name="pathway" required="required"/> <br/>
    <input type="submit" value="Enter"/>
</form>

Upvotes: 0

Noman
Noman

Reputation: 4116

your DB username is Null

Change $username = ""; to $username = "root";

Upvotes: 0

Related Questions