Store PHP code in MySQL database [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I have to send an email, the email can be formatted and stored in database. But the problem is I have to use some variable in the email to make it a dynamic email for each user.
INSERT INTO email (greeting) VALUES ( "Hello $name");
$coding = mysql_query("SELECT * FROM email ") or die(mysql_error());
$email = mysql_fetch_array( $coding );
echo $email['greeting'];
it should echo Hello PERSONS_NAME; like Hello John, But it echo Hello $name, as it is..
Does not work, Any help ???

You will probably be better off using placeholders and then swap out the placeholder with dynamic text when sending the email:
INSERT INTO email (greeting) VALUES ( "Hello %%NAME%%");
$coding = mysql_query("SELECT * FROM email ") or die(mysql_error());
$email = mysql_fetch_array( $coding );
$email['greeting'] = str_replace('%%NAME%%', $name, $email['greeting']);
echo $email['greeting'];

Related

Correct verification of request [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
I think I miss something about the verification of my PHP request. I'm actually doing $result = $req->fetch(); but result always return false for some reason.
$bdd = new PDO($conStr,$user,$pass);
$req = $bdd->prepare('INSERT INTO users(login, password, dateregister) VALUES(?, ?, NOW())');
$req->execute(array($loginR,$passwordR));
$result = $req->fetch();
Check the return value of execute function for success/failure message
$res = $req->execute(array($login_fieldR,$password_fieldR));
if (!$res )
{
echo '<p id="popup_text">Problem..</p>';
}
else
{
echo '<p id="popup_text">You are registered ! You can logged in now';
}

Using "a href" inside echo [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I'm trying to print a list of links, this means I need to use a href in the 3rd line, but I don't know how the syntax should be in this situation. Also, the link is a PHP variable.
$sql = mysql_query("select nm_linha from linhas where cidades_origem like '%$cod_cidades%'");
while($exibe = mysql_fetch_assoc($sql)){
echo $exibe['nm_linha'] .'<br>';
}
$sql = mysql_query("select nm_linha from linhas where cidades_origem like '%$cod_cidades%'");
while($exibe = mysql_fetch_assoc($sql)){
echo "<a href='link.html'>".$exibe['nm_linha'] ."</a><br>";
}
$sql = mysql_query("select nm_linha from linhas where cidades_origem like '%$cod_cidades%'");
while($exibe = mysql_fetch_assoc($sql)){
echo '<a href='".$exibe['nm_linha']."'>'.$exibe['nm_linha'].'</a><br>';
}
this is going to list the links as well as , on clicking it is redirected to th respective link.
try this.

Php string replace with different values each iteration through a loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I searched for an answer for this and could not find one. Sorry if there is already one out there. Here's my problem: I have a list of names in a database. Then I have a message that I want to display to each of them. I want to string replace the -user- with the name. Here's my code:
$message = "Hello, -user-";
$getusers = mysql_query("SELECT * FROM users");
while ($row = mysql_fetch_array($getusers)){
$username = $row["username"];
//replace -user- with the actual username
$message = str_replace("-user-", $username, $message);
echo $username;
echo $message;
}
Now say I have 3 users in my database: dan, bob, and jill.
Here is what it displays:
dan Hello, dan bob Hello, dan jill Hello, dan.
What I want it to display:
dan Hello, dan bob Hello, bob jill Hello, jill.
Then you have to reset message every time
$message = "Hello, -user-";
through the loop
Move
$message = "Hello, -user-";
into the while loop to start from the same $message each time. Otherwise only first , "dan" is present in message from the first iteration and replacement.

I get error when i use mysqli select [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
<?php if($_POST) {
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);
$mysqli = new mysqli('localhost','root','','movie_posters');
$query = $mysqli->query("SELECT password FROM users WHERE username = '"$username"'");
} ?>
When I try this code on WAMP, I get error like; this http://i.stack.imgur.com/qcifR.jpg
What can I do ?
Do not use single and double quotes in your query.
This is the right way:
$query = $mysqli->query("SELECT password FROM users WHERE username = '$username'");
Otherwise, you will not print $username's value.
You have to put periods before and after you variable.
In your example:
$query = $mysqli->query("SELECT password FROM users WHERE username = '".$username."'");

If email exists return error else add php mysql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to write a script that does the following. Takes a users email and added's it to a database however if the users email already exists it rejects.
<?
require_once('includes/db.php');
$email = mysqli_real_escape_string($link, $_POST['email']);
$dupesql = "SELECT * FROM emails where (email = '$email')";
$duperaw = mysqli_query($link, $dupesql);
if(int mysql_num_rows($duperaw) > 0){
echo 'Error already in there';
}
else {
$sql = "INSERT INTO emails(email)
VALUES('$email')";
$result = mysqli_query($link, $sql);
header('Location: poll/poll.php');
}
// close mysql
mysqli_close($link);
?>
Why not let mysql take care of it? When you already want email to be unique, then define an unique index for email :
CREATE UNIQUE INDEX email_index ON emails (email)
now you only have to check if any rows has been affected after insert :
if (mysqli_affected_rows()>0) {
//success, email inserted
} else {
// rejected
}
By that you'll spare a call to the database and make the code more simplistic, imho.
try this
$mysqli->real_query('INSERT INTO '.$table.' ('.$cols.') VALUES ('.$vals.')');
Change your code as follow:
$dupesql = "SELECT * FROM emails where email = '$email'";
$duperaw = mysqli_query($link, $dupesql);
if(mysql_num_rows($duperaw)==1){
echo 'Error already in there';
}
You are mixing mysqli_ with mysql_ which is the main problem here.
Use mysqli_num_rows instead of mysql_num_rows
You should not mix mysqli() and mysql().
And remove int error casting.
//...
if(mysqli_num_rows($duperaw) > 0){
echo 'Error already in there';
}
//...

Categories