LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2019, 02:51 PM   #1
pr0xibus
Member
 
Registered: Apr 2004
Location: Montrose, Scotland
Distribution: Slackware
Posts: 161

Rep: Reputation: 24
PHP / HTML Simple test


Evening folks.

I am trying to pass simple details from one page to another and then add to an sqldb, but at the moment I am struggling to even pass a simple firstname/lastname from one page to another, any ideas where I am going wrong?

index.php
Code:
<html>
<body>


<form action="insert.php" method="POST">
Firstname: <input type ="text" name="first_name" /><br><br>
Lastname: <input type ="text" name="last_name" /><br><br>

<input type="submit">
</form>


</body>
</html>
insert.php
Code:
<html>
<body>
//testonly too see if data has been passed
Hello <?php echo $_POST["first_name"];?><br>
Hello <?php echo $_POST["last_name"];?><br>

<?php
$hostname = "127.0.0.1";
$username = "******";
$password = "******";
$db = "test_db";

$dbconnect=mysqli_connect($hostname,$username,$password,$db);


if ($dbconnect->connect_error)
{
	die("DB Connection Failed: " . $dbconnect->connect_error);

}

if (isset($_POST['submit']))
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];

$query= ("INSERT INTO test_tbl (first_name,last_name) VALUES ('$first_name','$last_name')");

if (!mysqli_query($dbconnect, $query))
{
die('An error');
}
else
{
echo " Added Successfully";
}


?>
</body>
</html>
 
Old 11-08-2019, 03:13 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 22,068

Rep: Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887
Quote:
Originally Posted by pr0xibus View Post
Evening folks.
I am trying to pass simple details from one page to another and then add to an sqldb, but at the moment I am struggling to even pass a simple firstname/lastname from one page to another, any ideas where I am going wrong?
index.php
Code:
<html>
<body>

<form action="insert.php" method="POST">
Firstname: <input type ="text" name="first_name" /><br><br>
Lastname: <input type ="text" name="last_name" /><br><br>

<input type="submit">
</form></body></html>
insert.php
Code:
<html>
<body>
//testonly too see if data has been passed
Hello <?php echo $_POST["first_name"];?><br>
Hello <?php echo $_POST["last_name"];?><br>

<?php
$hostname = "127.0.0.1";
$username = "******";
$password = "******";
$db = "test_db";

$dbconnect=mysqli_connect($hostname,$username,$password,$db);

if ($dbconnect->connect_error)
{
	die("DB Connection Failed: " . $dbconnect->connect_error);

}

if (isset($_POST['submit']))
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];

$query= ("INSERT INTO test_tbl (first_name,last_name) VALUES ('$first_name','$last_name')");

if (!mysqli_query($dbconnect, $query))
{
die('An error');
}
else
{
echo " Added Successfully";
}

?>
</body></html>
I did something similar not long ago; was for MySQL, and I did it on one page, but this is the 'meat' of it:
Code:
<?php
$state = $_GET['st'];

// Connect to the database, or die if you can't
// **NOTE** PHP5 connections need mysql_xxx, vs. PHP7 with mysqli_ Connection strings for PHP 5 commented out below.
// $link = mysqli_connect("127.0.0.1", "user", "password") or die ("Could not connect to database; contact administrator.<BR>");
// $db = "wp_smaorg";
// mysqli_select_db($link, $db) or die ("Could not select database; contact administrator<BR>");
$link = mysqli_connect("127.0.0.1", "user", "password","database") or die ("Could not connect to database; contact administrator.<BR>");

// SQL Query
$sql1 = "select something from some field where something else='this';";
$result1 = mysqli_query($link, $sql1);
while($row1 = mysqli_fetch_array($result1)){
    $FieldOne = $row1['FieldOne'];
    $FieldTwo = $row1['FieldTwo'];
   .......(whatever else)........
}
Probably a zillion other/better ways to go, but this works for my situation. Passing the 'st' as the typical "?st=variable" in the browser, so slightly different than what you're doing.

Last edited by TB0ne; 11-08-2019 at 03:26 PM.
 
Old 11-08-2019, 03:21 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,875

Rep: Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353
@OP: Did you get an error message?Would you mind quoting it?

PS: if the error message not informative enough, try this function: https://www.w3schools.com/php/func_mysqli_error.asp

Last edited by NevemTeve; 11-08-2019 at 03:26 PM.
 
Old 11-08-2019, 03:27 PM   #4
pr0xibus
Member
 
Registered: Apr 2004
Location: Montrose, Scotland
Distribution: Slackware
Posts: 161

Original Poster
Rep: Reputation: 24
Evening NevemTeve

Unfortunately no error, Just a blank page, Not really fussed about the sql part ill sort that but i'd expect even the first part of insert.php to work

<html>
<body>
//testonly too see if data has been passed
Hello <?php echo $_POST["first_name"];?><br>
Hello <?php echo $_POST["last_name"];?><br>
 
Old 11-08-2019, 03:32 PM   #5
pr0xibus
Member
 
Registered: Apr 2004
Location: Montrose, Scotland
Distribution: Slackware
Posts: 161

Original Poster
Rep: Reputation: 24
So opening the site on another browser, I seem to get an error 500.

Looking at httpd logs i get [Fri Nov 08 20:31:25.446487 2019] [php7:emerg] [pid 9220:tid 140095550105344] [client 127.0.0.1:60922] PHP Parse error: syntax error, unexpected end of file in /var/www/htdocs/insert.php on line 43, referer: http://localhost/index.php
 
Old 11-08-2019, 03:35 PM   #6
pr0xibus
Member
 
Registered: Apr 2004
Location: Montrose, Scotland
Distribution: Slackware
Posts: 161

Original Poster
Rep: Reputation: 24
Data now shows after removing some code

Code:
Hello <?php echo $_POST["first_name"];?><br>
Hello <?php echo $_POST["last_name"];?><br>
<?php
$hostname = "127.0.0.1";
$username = "root";
$password = "G4l4xy100";
$db = "test_db";

$dbconnect=mysqli_connect($hostname,$username,$password,$db);


if ($dbconnect->connect_error)
{
	die("DB Connection Failed: " . $dbconnect->connect_error);

}

if (isset($_POST['submit']))
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];

$query= ("INSERT INTO test_tbl (first_name,last_name) VALUES ('$first_name','$last_name')");

}



?>
 
Old 11-08-2019, 03:39 PM   #7
captain_sensible
Member
 
Registered: Apr 2010
Posts: 142

Rep: Reputation: 13
@pr0xibus you've used the same handle twice

Firstname: <input type ="text" name="first_name" /><br><br>
Lastname: <input type ="text" name="last_name" /><br><br>

name=
name=

also why jump into the deep end ; have a go maybe with sqlite its serverless
 
Old 11-08-2019, 03:49 PM   #8
pr0xibus
Member
 
Registered: Apr 2004
Location: Montrose, Scotland
Distribution: Slackware
Posts: 161

Original Poster
Rep: Reputation: 24
Unsure if it makes a difference. First name and Last name show correctly with the above code.
 
Old 11-08-2019, 04:40 PM   #9
pr0xibus
Member
 
Registered: Apr 2004
Location: Montrose, Scotland
Distribution: Slackware
Posts: 161

Original Poster
Rep: Reputation: 24
Finally got it working with sql too

Posting the whole lot just incase someone needs the same thing in the future

If you have a form with fields, you can post it to the next page, and then input it into an SQL DB

index.html
Code:
html>
<body>


<form action="insert.php" method="post">
Firstname: <input type ="text" name="first_name" /><br><br>
Lastname: <input type ="text" name="last_name" /><br><br>

<input type="submit">
</form>


</body>
</html>
insert.php
Code:
?php
$hostname = "localhost";
$username = "user1";
$password = "******";
$db = "test_db";

$dbconnect = mysqli_connect($hostname , $username , $password, $db) or die ("Could not connect to DB");

echo nl2br ("Connected Successfully \n");

if (isset($_POST['first_name']))
{
        echo nl2br ("Data present \n");
        $first_name = $_POST["first_name"];
        $last_name = $_POST["last_name"];
        $query = "INSERT INTO test_tbl (first_name, last_name) VALUES ('$first_name', '$last_name')";

        if (mysqli_query($dbconnect,$query))
        {
                echo nl2br ("New Record Created \n");
        }

        else
        {
                echo nl2br ("No Record Created \n");
        }

}

else
{
        echo "No data present from Site";
}




mysqli_close($dbconnect);

?>
 
1 members found this post helpful.
Old 11-09-2019, 09:00 AM   #10
Steve R.
Member
 
Registered: Jun 2009
Location: Morehead City, NC
Distribution: Ubuntu 16.04
Posts: 398

Rep: Reputation: 73
Quote:
Originally Posted by pr0xibus View Post
Posting the whole lot just incase someone needs the same thing in the future
I have experienced this type of problem in the past. So at the time I was attempting to find a solution, it proved somewhat difficult since few posts got into how to pass values between pages. I hope that others, with this type of issue, will find this post. Thanks for posting.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Embedded PHP in HTML- either PHP ignored or HTML broken vltreude Programming 1 12-10-2012 03:35 AM
Load test, boundary test & stress test for USB EHCI/xHCI driver rama_toshiba Linux - Kernel 5 02-29-2012 03:43 PM
[SOLVED] Silencing the line "echo test > test/test.txt" in a shell script Arenlor Linux - General 2 06-18-2010 02:37 PM
PHP 5.2.6 "make test" zend test failures mlnutt Linux - Server 0 05-07-2008 03:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration