Same PHP script works only with 1 of 2 SQL tables

Alles, was PHP betrifft, kann hier besprochen werden.

Same PHP script works only with 1 of 2 SQL tables

Postby Lukas M » 30. July 2019 11:39

Hi,

I hope that this will be very easy for somebody to figure out. I have a very simple script that parses inputs from 3 text fields in a HTML page to a myPHP Database called "test" and within to a "table 1" . I created a new table (Table 2) with different column names, adjusted the previous HTML page with a correct input field names, adjusted the action post PHP file with a new variables - and its not working. I checked several times for missing quotes, brackets etc, found nothing.

whenever I click in the second HTML document on "submit" i can see a new ID entry in a Table 2 like 1 .. 2 .. 3 but other two columns are empty and there are no data. The same script in original HTML page works and fills the Table 1 with data. There are no errors on the screen.
Lukas M
 
Posts: 19
Joined: 28. July 2019 15:07
XAMPP version: 7.3.7
Operating System: Windows 7 Pro 64bit SP

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Nobbie » 30. July 2019 16:13

Lukas M wrote:I checked several times for missing quotes, brackets etc, found nothing.


Check again, more carefully. You must have overlooked something.

You can insert echo-Statements for debugging reasons in order to see, if the variables are having the contents you expect. That distracts your screen, but its only for testing purpose, later on, when you found your error, you can remove the echo's. Thats the way how every PHP/HTML programmer has to go, you have to learn how to debug your scripts in order to find your errors.

Of course it is NOT easy for somebody to figure out, what you did overlook, as definately nobody sees your code and nobody has a clue, what you are doing. Nobody can read your mind.
Nobbie
 
Posts: 13171
Joined: 09. March 2008 13:04

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Lukas M » 30. July 2019 16:31

thanks for the reply. Im all new to PHP / SQL.. I thought whether having the first column ID resp. IDs as INT primary type with autoincreasement could stand behind the issue as having in one table records starting 1 and in the second also starting with 1 would literally "beat between each other which one is unique", but that does not seem to be the issue as first table contains 14 rows and second table now 16 and still only the ID number is filled in. Below is the code for first table:

DB_CONNECTION.php

<?php

$dbServername = "127.0.0.1";
$dbUsername = "root";
$dbPassword = "";
$dbName = "tutorial";

$con = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
?>


PHP SCRIPT FOR HTML PAGE 1
<?php
include_once 'db_connection.php';

$regUsername = $_POST['regUsername'];
$regEmail = $_POST['regEmail'];
$regPassword = $_POST['regPassword'];


$sql = "INSERT INTO accountcreationdb (regUsername, regEmail, regPassword) VALUES ('$regUsername','$regEmail','$regPassword');";


mysqli_query($con,$sql);

header("Location: step2.html");

?>

PHP SCRIPT FOR PAGE 2

<?php
include_once 'db_connection.php';

$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$Street = $_POST['Street'];

$sql = "INSERT INTO accountsales (FirstName, LastName, Street) VALUES ('$FirstName','$LastName','$Street');";

mysqli_query($con,$sql);

header("Location: start.html");

?>


HTML PAGE 2
<html>
<head>
</head>

<title></title>
<body>

<form action="includes3.php" method="post">

Name:
<input id="FirstName" type="text">

<BR>Last Name:
<input id="LastName" type="text">

<BR>Street:
<input id="Street" type="text">

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

</body></html>
Lukas M
 
Posts: 19
Joined: 28. July 2019 15:07
XAMPP version: 7.3.7
Operating System: Windows 7 Pro 64bit SP

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Lukas M » 30. July 2019 16:43

I Found It.. (thanks to the scripts next to each other). The problem rested with the input field specification.

First working HTML page / Table has <INPUT NAME="xxxxx" .. and that works.. while the 2nd page has <INPUT ID="xxxx" and that was the reason why not entries were saved into dbase
Lukas M
 
Posts: 19
Joined: 28. July 2019 15:07
XAMPP version: 7.3.7
Operating System: Windows 7 Pro 64bit SP

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Nobbie » 30. July 2019 16:44

1) I cannot see which code is stored in which file (filename)?! What is "PHP SCRIPT FOR PAGE 2"??? What is "HTML PAGE 2"??? In the action-Tags you specify filenames, but i cannot see which code belongs to which file?!!

2) DO NOT USE header()-function, that is totally wrong. What do you want to establish there? As i cannot see how the workflow/dataflow is going (due to point 1, as i have no idea which code belongs to which file), i dont know, what you want to achieve. But using header()-function with Location: is totally wrong anyway.

You may use header() Function in future in some cases, but not before you really know and understand, what is happening there. For now i am absolutely sure, you do not have the slightest idea what that function does and how it works. Probably it is the reason for all your problems.

P.S.: i already sent this answer before you wrote the last posting. I still dont understand what you are doing. And you should not use header() function. You have to learn, how PHP and Forms/HTML/HTTP is working.
Nobbie
 
Posts: 13171
Joined: 09. March 2008 13:04

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Lukas M » 30. July 2019 17:44

Interesting . why should I not user HTML tag <HEAD>? In all respected PHP tutorial on youtube its there.
Lukas M
 
Posts: 19
Joined: 28. July 2019 15:07
XAMPP version: 7.3.7
Operating System: Windows 7 Pro 64bit SP

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Altrea » 30. July 2019 17:52

PHP header() function is not the same as HTML <HEAD> tag
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Nobbie » 30. July 2019 19:09

Funny - as Altrea said, HTML HEAD Tag is not the same as PHP header() function.

The header() function is a sophisticated function to manipulate the HTTP Header, which is part of the HTTP Protocoll. The HTTP Protocoll is the communication between the browser and the server and takes place hidden to the user.

In your case, mostly a simple "include()" would be the better solution, most important it keeps the context (all request variables like $_GET, $_POST, $_COOKIES etc.), whereas header() totally looses the context and results in a completely new HTTP request. in short: You do not only trigger a new HTTP request, you also loose all data. Mostly not wanted.
Nobbie
 
Posts: 13171
Joined: 09. March 2008 13:04

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Lukas M » 30. July 2019 19:17

aha.. so you are referrring to this redirection line at the bottm of the PHP script.

header("Location: start.html");

right?
Lukas M
 
Posts: 19
Joined: 28. July 2019 15:07
XAMPP version: 7.3.7
Operating System: Windows 7 Pro 64bit SP

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Nobbie » 30. July 2019 19:24

I will give you a very small example and without any explanation. Simply run it on your Xampp installation:

1) First file is page1.php. We have in two flavours, one does an include(), the other does a header("Location:"). Lets see:

page1.php;

Code: Select all
<?php
     $name = "Donald";
     $fname = "Duck";

    header("Location: page2.php");
?>


And we have another page1.php, lets call it page1i.php (i for include):

page1i.php:

Code: Select all
<?php
     $name = "Donald";
     $fname = "Duck";

    include("page2.php");
?>


And finally, we have page2.php:

page2.php

Code: Select all
<?php
     echo "Welcome $name! Your familiy name is $fname";
?>


Put the files into your htdocs folder, then run page1.php and after run page1i.php

See the difference?!
Nobbie
 
Posts: 13171
Joined: 09. March 2008 13:04

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Altrea » 30. July 2019 19:32

There is one situation where a header() redirect is part of the design pattern to prevent unwanted double execution post transmitions.
The design pattern is named "post-redirect-get" and does exactly what it is named for. You can google it.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Lukas M » 31. July 2019 19:09

[quote="Nobbie"]I will give you a very small example and without any explanation. Simply run it on your Xampp installation:


And we have another page1.php, lets call it page1i.php (i for include):

page1i.php:

Code: Select all
<?php
     $name = "Donald";
     $fname = "Duck";

    include("page2.php");
?>


And finally, we have page2.php:

page2.php

Code: Select all
<?php
     echo "Welcome $name! Your familiy name is $fname";
?>


The script with "include" takes the variables with it to a final landing page, while the one with "header" somehow "drops them" and the final page is full of undefined variables. That was what you wanted to show me?
Lukas M
 
Posts: 19
Joined: 28. July 2019 15:07
XAMPP version: 7.3.7
Operating System: Windows 7 Pro 64bit SP

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Nobbie » 31. July 2019 22:46

Yes.
Nobbie
 
Posts: 13171
Joined: 09. March 2008 13:04

Re: Same PHP script works only with 1 of 2 SQL tables

Postby Lukas M » 31. July 2019 22:55

thank you for sharing this knowledge with me. I really appreciate it.
Lukas M
 
Posts: 19
Joined: 28. July 2019 15:07
XAMPP version: 7.3.7
Operating System: Windows 7 Pro 64bit SP


Return to PHP

Who is online

Users browsing this forum: No registered users and 13 guests