What's wrong with this link? [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
We are using this link to delete data from an MySQL database, but when we launch it in the browser we get an error that something is wrong with this line.
Could someone say what's wrong with it?
echo "<a href=delete_hardware.php?id=<?php.$rows['hardwareID']; ?>>delete</a>";

Here is the correct link, with quotes and concatenation :
echo 'delete';

Try this:
echo "delete";

The problem is that you're using a link to perform a destructive action.
Never, ever, ever, EVER use <a href...> to perform an action. Especially not a "delete" action.
GET requests imply that they may be performed at any time, without any need for confirmation. POST requests, on the other hand, imply one-time use. This is why when you reload a page you opened through a link, that's fine, but try to reload a page you submitted with a form? HOLD ON THERE, are you sure you want to resubmit because you might end up buying the same item twice!
Therefore, your code should be:
?>
<form action="delete_hardware.php" method="post">
<input type="hidden" name="id" value="<?=intval($rows['hardwareID'])?>" />
<input type="submit" value="delete" onClick="return confirm('Are you SURE you want to delete this?');" />
</form>
<?php

Related

How to modify a link with an user ID [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
<img src="./images/w.jpg">
Hello, I'm new to coding and I was wondering how can I make a custom link with an user ID directly taken from my database. The name usersId is a column in a mySQL table and I take that usersId from a session. Can you please help me fix the problem
I tried to use a $_SESSION to insert the user id in the link but it seem that it didn't work and when I open a session on my website it just removes the $_SESSION and give me a link without the user id at the end.
You need to break out of the PHP mode in order to echo the session variable:
<a href="https://123.com/<?= htmlentities($_SESSION['usersId']) ?>" target="_blank">
<img src="./images/w.jpg">
</a>
Note that I also used htmlentities() to encode the ID as an HTML attribute value, to avoid XSS vulnerabilities.
I strongly recommend you do not learn HTML by writing it as strings in PHP - learn proper HTML first, then inject dynamic values with PHP where needed. Mixing the two like you did just leads to a mess.
Have you defined your session object?
It should look like this;
$_SESSION["userId"] = your_mysql_object["userId"]

PHP - Variable from form not staying after refresh, or not appearing at all [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am testing PHP and the variable from the form is not staying, and does not appear even on the same page. Here is the code:
<?php
echo "<form action='input.php' method='post'>Test: <input type='text' name='last' </form><input type=submit>";
echo "<h1>The last input was: </h1>", $_POST["last"];
?>
This is not a PHP problem. It is an HTML problem.
Your submit button is outside of the form.
It's effectively "submitting" a blank, empty, made-up form of its own.
That made-up form does not contain any of your data.
Move the submit button to inside of the form.

Calling a PHP variable through button onclick [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
The goal is to call the variable $url when the button is clicked. With this current bit of code, when the button is clicked nothing happens and the error says unexpected token.
<?php
$url = "edit_beginningcakephp.php?title=$title&author=$author&isbn=$isbn
&year=$year&pages=$pages&price=$price";
>?
<input type="button" value="Edit Book" onclick="window.location.href='<?= $url ?>'">
I suppose you're not correctly encoding the content of the variables (known as XSS)
You need to urlencode the content of your variables. Also, & needs to be entered as an HTML entity &.
Try:
$url = 'edit_beginningcakephp.php?title='.urlencode($title).'&author='.urlencode($author).'&isbn='.urlencode($isbn).'&year='.urlencode($year).'&pages='.urlencode($pages).'&price='.urlencode($price);
PS: >?should be ?> and make sure the URL doesn't contain any newlines.
" >? "
That looks like it might be a problem, definitely throws a syntax error on my local :)
While MrTux's answer is very good and his advice should surely be followed, the actual problem with your code is the return in the URL. URLS can't contain returns!
Solution: delete the return. And the spaces. And follow MrTux's advice.

Php implimentation of Online Examination system [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am a newbie PHP devloper and i want to implement an online examination system. I want to implement a system which asks one question at a time i.e after answering 1st question click newxt then 2nd question will be displayed. how to implement this? do i have to implement through sessions? I am using loop but loop displays 20 questions one after another plzz help.
A very easy way to do this would be to implement a html structure that is the same for each questions.
In php, at the beginning of the page, you just check for the post data, if there's none, then you load the first question ( in database, or hardcodded, as you wish ).
If there's post data, you check if the question id is there, save the answer for the user, and load the next question.
It would look a bit like that:
<?php
function loadQuestion($id){
//your logic to load the question
return $question;
}
function saveAnswer($id,$answer){
//your logic to save the answer
}
if($_POST){
saveAnswer($_POST['id_question'],$_POST['answer'])
$data = loadQuestion($_POST['id_question']+1);
}
else $data = loadQuestion(1); //first question
?>
<form method="POST">
<input type="hidden" name="id_question" value="<?php echo $data["id_question"]; ?>" />
<div class="questionDiv">
<?php echo $data["question"]; ?>
</div>
answer:
<input type="text" value="" name="answer" />
<input type="submit" value="send" />
</form>
You could use sessions, yes. Using sessions would allow you to write all of your questions and everything in a single page, and then you could use a switch/case block to pump out the right question within the page. Use the post method on the form, and at the top of your page, check if the post is set, push the data into the correct mysql statement if so, and display the next question question (based on a session variable).

Can someone fix my php echo statement? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
echo "Click Here";
Can someone please fix this? I keep getting errors and i really am confused...
I'm trying to make it so it echos a changed url using a form.
My form:
<body>
<h1>Enter Player Name.</h1>
<form method="post" action="handler.php">
<input type="text" name="player">
<input type="submit">
</form>
</body>
If you can, can you tell me how to make it so when the "Submit" button is pressed it auto redirects them in a new tab to the edited url? If you could, it would be amazing!
This should do the trick. When using quotation marks you can put the PHP variable direction inside the string. Then you can change the quotes around the url to single quotes so it doesn't close out of the string.
echo "<a href='http://bans.scavengercraft.com/index.php?action=viewplayer&player=$_POST[player]&server=0'>Click Here</a>";

Categories