Php string replace with different values each iteration through a loop [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 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.

Related

My POS application is inserting records twice into the database [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 11 months ago.
Improve this question
I have been trying to insert into my database but it inserts with a random behavior. Although, the code runs correctly on PHP Version 7., I'm having problems with running it on PHP Version 8.1.2.
Sometimes it inserts correctly, most times it inserts twice. That's my only challenge actually. I also tried to fix by reporting errors using || ob_start(); ini_set('display_errors', 1); || and also disabled the javascript alert but no error was displayed. I'd appreciate it if you could look at the code a little closer. I checked the error logs and there's no problem.
if(isset($_POST['submit_sales'])){
$prd = $_POST['prd'];
$count = count($prd);
$rand_sales = rand();
$cust_name = check_input($_POST['custname']);
$cust_num = check_input($_POST['custnumber']);
// echo $count;
// Looping all files
if($count<1){
// $error = 'Cart is empty';
echo '<script>alert("Cart is empty !!!");window.location="all_products.php";</script>';
// echo '<script>window.location="all_products.php";</script>';
}else{
for($i=0;$i<$count;$i++){
$products = $_POST['prd'][$i];
$price = $_POST['price'][$i];
$qty = $_POST['qty'][$i];
$fl= dbconnect()->prepare("INSERT INTO sales SET cust_name=:custname, cust_number=:custnumber, prd_name=:prd, qty=:qty, price=:price, ref_code=:random");
$fl->bindParam(':custname', $cust_name);
$fl->bindParam(':custnumber', $cust_num);
$fl->bindParam(':prd', $products);
$fl->bindParam(':qty', $qty);
$fl->bindParam(':price', $price);
$fl->bindParam(':random', $rand_sales);
$fl->execute();
}
if($fl){
echo "<script>alert('doneit');window.location='all_products.php';</script>";
}else {
echo "<script>alert('Error Inserting Into Database')</script>";
}
}
}
I had included a script to reload the page in my header hence the double inserting, upon discarding the script, my problem was fixed. Thank you all for helping me with my challenge

More than one preg_match queries? [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 6 years ago.
Improve this question
I am a newbie to php and trying to write the code that validates my form.now the problem is I am having trouble in this line of code:
$fullname_pattern = "/[a-zA-Z]+/";
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$email_pattern = "/^[a-zA-Z]+([a-zA-Z0-9]+)?#[a-zA-Z]{3,50}\.(com|net|org)/";
$password = $_POST['password'];
$password_pattern = "/(.){6,12}/";
if(preg_match($fullname_pattern,$fullname)) &&
(preg_match($email_pattern,$email)) &&
(preg_match($password_pattern,$password))
{
header("Location: home.php");
}
else
header("Location: registration.php");
Don't know what to do!
if(preg_match($fullname_pattern,$fullname))
Remove Last ")"
And add here (preg_match($password_pattern,$password)))
Like this:
if(preg_match($fullname_pattern,$fullname)
&& preg_match($email_pattern,$email)
&& preg_match($password_pattern,$password)
) {

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.

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."'");

Store PHP code in MySQL database [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
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'];

Categories