0

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);
    ?>

4 Answers 4

1

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);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

That all works thanks. If that person has logged in as Bob, how do i get the pathway to be inserted in the same row as where Bob's username is stored. At the moment it is entered in a new row.
Update users set pathway = '$pathway' where username = 'BOB' --- this works only if Bob is already in database. if not the you shoud insert it as in the example i posted.
0

your DB username is Null

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

Comments

0

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>

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.