/    Sign up×
Community /Pin to ProfileBookmark

page doesn’t send Post into to another page

I have two PHP pages that interact. The first one I select data and present it to th epage for editing. Each row has a submit that references an update page. Every submit works to update the DB EXCEPT for the first row. I set the update page to print the post data and to refresh for 1 second so I could see the post data. Editing the first row does not produce any post data in the second page. So I must have an error in the 1st page?

Here is the 1st page:

[code=html]<!DOCTYPE html>
<html>
<head>
<Title>Edit Template Settings</title>
</head>
<body>
<p>CQ Simple Phone Provisioning System</p>
<hr>

<form action=”” method=”post”>
Template name to edit: <input type=”text” name=”tname”><br><input type=”submit”>

<?php
$tempname = $_POST[‘tname’];
echo $tempname;
$con = mysqli_connect(‘localhost’,’root’,”);
$sql = “Select * from cqadmin.keys where templatename = ‘$tempname'”;
$records = mysqli_query($con,$sql);
?>
<table>
<tr>
<th></th>
<th></th>
<th>Key Name</th>
<th>Key Type</th>
<th>Value</th>
<th>Label</th>
</tr>
<?php
while($row = mysqli_fetch_array($records))
{
echo “<tr><form action=update.php method=post>”;
echo”<td><input type=hidden name=id value='”.$row[‘id’].”‘></td>”;
echo”<td><input type=hidden name=name value='”.$row[‘templatename’].”‘></td>”;
echo”<td><input type=hidden name=keyid value='”.$row[‘keyid’].”‘></td>”;
echo”<td><input type=text name=key value='”.$row[‘keyname’].”‘><Readonly></td>”;
echo”<td><input type=text name=type value='”.$row[‘keytype’].”‘></td>”;
echo”<td><input type=text name=value value='”.$row[‘keyvalue’].”‘></td>”;
echo”<td><input type=text name=label value='”.$row[‘keylabel’].”‘></td>”;
echo “<td><input type=submit>”;
echo”</form></tr>”;
}

?>
</body>
</html>

[/code]

And the UPdate page

[code=html]<?php

//Connect to MYSQL
$conn = mysqli_connect(“localhost”,”root”,””);
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}

print_r($_POST);

$sql = “UPDATE cqadmin.keys SET templatename=’$_POST[name]’,keyname=’$_POST[key]’,keyid=’$_POST[keyid]’,keytype=’$_POST[type]’,keyvalue=’$_POST[value]’,keylabel=’$_POST[label]’ where id=’$_POST[id]'”;

//Execute the query
if ($conn->query($sql) === TRUE) {
header(“refresh:10; url=keys.php”);
} else {
echo “Error: ” . $sql . “<br>” . $conn->error;
}
$conn->close();

?>
[/code]

Can someone tell me what is going on? I have tried different browsers. There are no errors

Thanks

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmAug 06.2017 — You are missing a TON of quotes in your html. You might want to clean that up.
Copy linkTweet thisAlerts:
@edlentzauthorAug 06.2017 — You are missing a TON of quotes in your html. You might want to clean that up.[/QUOTE]

I know My table is messed up. What quotes? the page works, well except for the main reason I posted..
Copy linkTweet thisAlerts:
@rootAug 07.2017 — You should also format your form attributes, a <form tag in many servers needs the [B]action[/B] to point to the receiving page, the [B]enctype[/B] needs specifying and all good forms have a [B]name[/B] (not an id) and some use the [B]onsubmit[/B] to run a client side check to see if fields required are set (although now you can specify that in HTML5 inputs these days, its good for a fallback position where an older browser is used to view your site) before allowing the form to post, it is also good to have these details set so that if JavaScript is not available then the form will still post.

Sanitize, Sanitize, Sanitize... NEVER!!! use $_POST directly in a database setting, a good method of dealing with web data is to only accept specific fields, check that the form is present and the request method is a post and I use a system of salting my forms so that I know where and when the form was issued and to what IP address. Always put sanitized data into an array so that you know that anything you access in that sanitized array is safe(er) and hasn't updated the form field as can happen when the server is bombarded with multiple requests.

I would also use a session object per page to pass data relating to the current user, the time they got the page (so you can know how long and expire the page) and push them to login after 5 minutes inactivity (for example)
Copy linkTweet thisAlerts:
@ginerjmAug 07.2017 — Proper HTML calls for attributes to be wrapped in quotes. In your example, this
[code=html]
echo "<tr><form action=update.php method=post>";
echo"<td><input type=hidden name=id value='".$row['id']."'></td>";

[/code]


should be:
[code=html]
echo "<tr><form action='update.php' method='post'>";
echo"<td><input type='hidden' name='id' value='" . $row['id'] . "'></td>";

[/code]
Copy linkTweet thisAlerts:
@edlentzauthorAug 11.2017 — Well, I wrapped all the html attributes as suggested. Still the first record cannot be edited/updated. All of what I am doing is on a closed system. The final goal here it to have a single button on the page to update all the records that are changed.
Copy linkTweet thisAlerts:
@rootAug 12.2017 — After spending some time looking at your code, there are some strande construction going on, for a start you're nesting a form in a form, your supposed to seperate forms if you have more than one per page.

[code=php]<form action="" method="post">
Template name to edit: <input type="text" name="tname"><br><input type="submit">

<?php
$tempname = $_POST['tname'];
echo $tempname;
$con = mysqli_connect('localhost','root','');
$sql = "Select * from cqadmin.keys where templatename = '$tempname'";
$records = mysqli_query($con,$sql);
?>[/code]


is later followed on in the script
[code=php]<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action=update.php method=post>";
echo"<td><input type=hidden name=id value='".$row['id']."'></td>";

echo"<td><input type=hidden name=name value='".$row['templatename']."'></td>";[/code]
which is clearly a form


I would look at reworking the page.

You should also go on to the PHP website and read up about mysqli_ type queries, they are no like mysql_ function, instead of forming a statement, opening the database, running the query and then fetching the results as needed, you have to make your open your database, create the statement, execute the query, bind the result to variables then you can fetch your results.

http://php.net/manual/en/mysqli-stmt.fetch.php

Also, when using naming conventions, it is advised that you avoid using a reserved word or a keyword like calling the name of an input <input name="name" can lead to problems, I find my server hates "submit" as the name of the submit button, if I call it Submit, then the server is happy, so finding out about any requirements your host is imposing on you is an important step because your web form may require additional scripting from the hosting server, one host I used insisted that you used a library that they wrote for their servers to ensure that your webform, if using email, your email had to exist on the server if sending a confirmation form.

So in short, if something doesn't work on the server, then theres possibly a config issue, but looking at your script, you have some way to go in regards to outputing a web page and web form that isn't nested or likely to conflict with other forms (through nesting and overlapping of tags)
Copy linkTweet thisAlerts:
@edlentzauthorAug 12.2017 — Thank you for taking the extra time to look at my mess. I admit I have limited know how in this area. Ideally I want to keep all the work needed to be done on a single page. I will see if I can wrap my head around the link you provided
×

Success!

Help @edlentz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...