Correctly accessing entire row for database insertion with mysqli & PHP
up vote
-1
down vote
favorite
I'm working on a project where we are sending messages to users using a database. In this php file, I insert into the database with a to, from, date, and message field. The goal is to send the same message to all the people within one certain company. However, when I try sending it, the message will not send sometimes, and when it does, it will only message the first contact in the group/row that I get from the database. I've seen other examples online, but so far I haven't seen this exact problem with anyone else. Any suggestions would be appreciated.
$sql = 'SELECT drivers.username FROM project.drivers WHERE drivers.sponsor="'.$_SESSION["company"].'"'; //query to get all driver usernames within one company
$result = $conn->query($sql);
//$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){ //iterating throughout all the drivers
$to = $row["username"]; //username of current driver
$from = $_SESSION["username"]; //from current company
$message = $_SESSION["username"] . " updated your point to dollar ratio. Your new point to dollar ratio is " . $_SESSION["pointRatio"] . "."; //message
$sql2 = "INSERT INTO project.messages(messages.date, messages.to, messages.from, messages.message) VALUES(now(), '".$to."', '".$from."', '".$message."')"; //inserting message into database
$conn->query($sql2); //query for database
$to = $_SESSION["username"]; //this is the same format but sending the message to the company itself
$message = $_SESSION["username"] . " updated your point to dollar ratio. Your new point to dollar ratio is " . $_SESSION["pointRatio"] . ".";
$sql2 = "INSERT INTO project.messages(messages.date, messages.to, messages.from, messages.message) VALUES(now(), '".$to."', '".$from."', '".$message."')";
$conn->query($sql2);
}
php database session mysqli
|
show 7 more comments
up vote
-1
down vote
favorite
I'm working on a project where we are sending messages to users using a database. In this php file, I insert into the database with a to, from, date, and message field. The goal is to send the same message to all the people within one certain company. However, when I try sending it, the message will not send sometimes, and when it does, it will only message the first contact in the group/row that I get from the database. I've seen other examples online, but so far I haven't seen this exact problem with anyone else. Any suggestions would be appreciated.
$sql = 'SELECT drivers.username FROM project.drivers WHERE drivers.sponsor="'.$_SESSION["company"].'"'; //query to get all driver usernames within one company
$result = $conn->query($sql);
//$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){ //iterating throughout all the drivers
$to = $row["username"]; //username of current driver
$from = $_SESSION["username"]; //from current company
$message = $_SESSION["username"] . " updated your point to dollar ratio. Your new point to dollar ratio is " . $_SESSION["pointRatio"] . "."; //message
$sql2 = "INSERT INTO project.messages(messages.date, messages.to, messages.from, messages.message) VALUES(now(), '".$to."', '".$from."', '".$message."')"; //inserting message into database
$conn->query($sql2); //query for database
$to = $_SESSION["username"]; //this is the same format but sending the message to the company itself
$message = $_SESSION["username"] . " updated your point to dollar ratio. Your new point to dollar ratio is " . $_SESSION["pointRatio"] . ".";
$sql2 = "INSERT INTO project.messages(messages.date, messages.to, messages.from, messages.message) VALUES(now(), '".$to."', '".$from."', '".$message."')";
$conn->query($sql2);
}
php database session mysqli
3
Your code is a huge security risk!! You are vulnerable for sql-injection. Please read upon this and do the required steps to prevent it!!!!
– cramopy
Nov 8 at 10:05
1
Thank you for pointing it out, but I wasn't too concerned about it because it isn't for any commercial use. Just focused on learning more about mysqli and php, although I can definitely go back and fix it.
– linksergey
Nov 8 at 10:10
Well when you are just about learning and training it is as important as if it would be for any (non-) commercial use. Your goal shouldn't be fixing it only for this question, but learn how to do it the right way. This knowledge will be helpful all the way you go with sql.
– cramopy
Nov 8 at 10:12
What is the definition of themessages
table - are there any unique constraints?
– Nigel Ren
Nov 8 at 10:13
1
You're 100% right and I appreciate the advice. It's definitely something I will come back to, I was just stuck on this particular problem.
– linksergey
Nov 8 at 10:14
|
show 7 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm working on a project where we are sending messages to users using a database. In this php file, I insert into the database with a to, from, date, and message field. The goal is to send the same message to all the people within one certain company. However, when I try sending it, the message will not send sometimes, and when it does, it will only message the first contact in the group/row that I get from the database. I've seen other examples online, but so far I haven't seen this exact problem with anyone else. Any suggestions would be appreciated.
$sql = 'SELECT drivers.username FROM project.drivers WHERE drivers.sponsor="'.$_SESSION["company"].'"'; //query to get all driver usernames within one company
$result = $conn->query($sql);
//$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){ //iterating throughout all the drivers
$to = $row["username"]; //username of current driver
$from = $_SESSION["username"]; //from current company
$message = $_SESSION["username"] . " updated your point to dollar ratio. Your new point to dollar ratio is " . $_SESSION["pointRatio"] . "."; //message
$sql2 = "INSERT INTO project.messages(messages.date, messages.to, messages.from, messages.message) VALUES(now(), '".$to."', '".$from."', '".$message."')"; //inserting message into database
$conn->query($sql2); //query for database
$to = $_SESSION["username"]; //this is the same format but sending the message to the company itself
$message = $_SESSION["username"] . " updated your point to dollar ratio. Your new point to dollar ratio is " . $_SESSION["pointRatio"] . ".";
$sql2 = "INSERT INTO project.messages(messages.date, messages.to, messages.from, messages.message) VALUES(now(), '".$to."', '".$from."', '".$message."')";
$conn->query($sql2);
}
php database session mysqli
I'm working on a project where we are sending messages to users using a database. In this php file, I insert into the database with a to, from, date, and message field. The goal is to send the same message to all the people within one certain company. However, when I try sending it, the message will not send sometimes, and when it does, it will only message the first contact in the group/row that I get from the database. I've seen other examples online, but so far I haven't seen this exact problem with anyone else. Any suggestions would be appreciated.
$sql = 'SELECT drivers.username FROM project.drivers WHERE drivers.sponsor="'.$_SESSION["company"].'"'; //query to get all driver usernames within one company
$result = $conn->query($sql);
//$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){ //iterating throughout all the drivers
$to = $row["username"]; //username of current driver
$from = $_SESSION["username"]; //from current company
$message = $_SESSION["username"] . " updated your point to dollar ratio. Your new point to dollar ratio is " . $_SESSION["pointRatio"] . "."; //message
$sql2 = "INSERT INTO project.messages(messages.date, messages.to, messages.from, messages.message) VALUES(now(), '".$to."', '".$from."', '".$message."')"; //inserting message into database
$conn->query($sql2); //query for database
$to = $_SESSION["username"]; //this is the same format but sending the message to the company itself
$message = $_SESSION["username"] . " updated your point to dollar ratio. Your new point to dollar ratio is " . $_SESSION["pointRatio"] . ".";
$sql2 = "INSERT INTO project.messages(messages.date, messages.to, messages.from, messages.message) VALUES(now(), '".$to."', '".$from."', '".$message."')";
$conn->query($sql2);
}
php database session mysqli
php database session mysqli
edited Nov 8 at 11:41
Funk Forty Niner
80.3k124799
80.3k124799
asked Nov 8 at 10:03
linksergey
45
45
3
Your code is a huge security risk!! You are vulnerable for sql-injection. Please read upon this and do the required steps to prevent it!!!!
– cramopy
Nov 8 at 10:05
1
Thank you for pointing it out, but I wasn't too concerned about it because it isn't for any commercial use. Just focused on learning more about mysqli and php, although I can definitely go back and fix it.
– linksergey
Nov 8 at 10:10
Well when you are just about learning and training it is as important as if it would be for any (non-) commercial use. Your goal shouldn't be fixing it only for this question, but learn how to do it the right way. This knowledge will be helpful all the way you go with sql.
– cramopy
Nov 8 at 10:12
What is the definition of themessages
table - are there any unique constraints?
– Nigel Ren
Nov 8 at 10:13
1
You're 100% right and I appreciate the advice. It's definitely something I will come back to, I was just stuck on this particular problem.
– linksergey
Nov 8 at 10:14
|
show 7 more comments
3
Your code is a huge security risk!! You are vulnerable for sql-injection. Please read upon this and do the required steps to prevent it!!!!
– cramopy
Nov 8 at 10:05
1
Thank you for pointing it out, but I wasn't too concerned about it because it isn't for any commercial use. Just focused on learning more about mysqli and php, although I can definitely go back and fix it.
– linksergey
Nov 8 at 10:10
Well when you are just about learning and training it is as important as if it would be for any (non-) commercial use. Your goal shouldn't be fixing it only for this question, but learn how to do it the right way. This knowledge will be helpful all the way you go with sql.
– cramopy
Nov 8 at 10:12
What is the definition of themessages
table - are there any unique constraints?
– Nigel Ren
Nov 8 at 10:13
1
You're 100% right and I appreciate the advice. It's definitely something I will come back to, I was just stuck on this particular problem.
– linksergey
Nov 8 at 10:14
3
3
Your code is a huge security risk!! You are vulnerable for sql-injection. Please read upon this and do the required steps to prevent it!!!!
– cramopy
Nov 8 at 10:05
Your code is a huge security risk!! You are vulnerable for sql-injection. Please read upon this and do the required steps to prevent it!!!!
– cramopy
Nov 8 at 10:05
1
1
Thank you for pointing it out, but I wasn't too concerned about it because it isn't for any commercial use. Just focused on learning more about mysqli and php, although I can definitely go back and fix it.
– linksergey
Nov 8 at 10:10
Thank you for pointing it out, but I wasn't too concerned about it because it isn't for any commercial use. Just focused on learning more about mysqli and php, although I can definitely go back and fix it.
– linksergey
Nov 8 at 10:10
Well when you are just about learning and training it is as important as if it would be for any (non-) commercial use. Your goal shouldn't be fixing it only for this question, but learn how to do it the right way. This knowledge will be helpful all the way you go with sql.
– cramopy
Nov 8 at 10:12
Well when you are just about learning and training it is as important as if it would be for any (non-) commercial use. Your goal shouldn't be fixing it only for this question, but learn how to do it the right way. This knowledge will be helpful all the way you go with sql.
– cramopy
Nov 8 at 10:12
What is the definition of the
messages
table - are there any unique constraints?– Nigel Ren
Nov 8 at 10:13
What is the definition of the
messages
table - are there any unique constraints?– Nigel Ren
Nov 8 at 10:13
1
1
You're 100% right and I appreciate the advice. It's definitely something I will come back to, I was just stuck on this particular problem.
– linksergey
Nov 8 at 10:14
You're 100% right and I appreciate the advice. It's definitely something I will come back to, I was just stuck on this particular problem.
– linksergey
Nov 8 at 10:14
|
show 7 more comments
1 Answer
1
active
oldest
votes
up vote
-1
down vote
if you add var_dump and check records from database - are correct? Do you really get all rows?
$sql = 'SELECT drivers.username FROM project.drivers WHERE drivers.sponsor="'.$_SESSION["company"].'"'; //query to get all driver usernames within one company
$result = $conn->query($sql);
//$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){ //iterating throughout all the drivers
var_dump($row);
}
Checking it out in mysql workbench gives me all the rows, and using var dump gives me all of them too. That's part of what is stumping me.
– linksergey
Nov 8 at 10:20
OK - if you have all results try next thing - inside while display text of your sql inserts - are all correct?
– Tomas Macek
Nov 8 at 10:31
Still all correct. Every value in the row is called, the text for the sql insert is correct. It's just like insert I've done in other sections of my code. Is it something with the query call perhaps?
– linksergey
Nov 8 at 10:47
So in project.messages is same number of new records as number of $row? If the numbers match - isn't your problem in another part of your code?
– Tomas Macek
Nov 8 at 11:21
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
if you add var_dump and check records from database - are correct? Do you really get all rows?
$sql = 'SELECT drivers.username FROM project.drivers WHERE drivers.sponsor="'.$_SESSION["company"].'"'; //query to get all driver usernames within one company
$result = $conn->query($sql);
//$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){ //iterating throughout all the drivers
var_dump($row);
}
Checking it out in mysql workbench gives me all the rows, and using var dump gives me all of them too. That's part of what is stumping me.
– linksergey
Nov 8 at 10:20
OK - if you have all results try next thing - inside while display text of your sql inserts - are all correct?
– Tomas Macek
Nov 8 at 10:31
Still all correct. Every value in the row is called, the text for the sql insert is correct. It's just like insert I've done in other sections of my code. Is it something with the query call perhaps?
– linksergey
Nov 8 at 10:47
So in project.messages is same number of new records as number of $row? If the numbers match - isn't your problem in another part of your code?
– Tomas Macek
Nov 8 at 11:21
add a comment |
up vote
-1
down vote
if you add var_dump and check records from database - are correct? Do you really get all rows?
$sql = 'SELECT drivers.username FROM project.drivers WHERE drivers.sponsor="'.$_SESSION["company"].'"'; //query to get all driver usernames within one company
$result = $conn->query($sql);
//$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){ //iterating throughout all the drivers
var_dump($row);
}
Checking it out in mysql workbench gives me all the rows, and using var dump gives me all of them too. That's part of what is stumping me.
– linksergey
Nov 8 at 10:20
OK - if you have all results try next thing - inside while display text of your sql inserts - are all correct?
– Tomas Macek
Nov 8 at 10:31
Still all correct. Every value in the row is called, the text for the sql insert is correct. It's just like insert I've done in other sections of my code. Is it something with the query call perhaps?
– linksergey
Nov 8 at 10:47
So in project.messages is same number of new records as number of $row? If the numbers match - isn't your problem in another part of your code?
– Tomas Macek
Nov 8 at 11:21
add a comment |
up vote
-1
down vote
up vote
-1
down vote
if you add var_dump and check records from database - are correct? Do you really get all rows?
$sql = 'SELECT drivers.username FROM project.drivers WHERE drivers.sponsor="'.$_SESSION["company"].'"'; //query to get all driver usernames within one company
$result = $conn->query($sql);
//$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){ //iterating throughout all the drivers
var_dump($row);
}
if you add var_dump and check records from database - are correct? Do you really get all rows?
$sql = 'SELECT drivers.username FROM project.drivers WHERE drivers.sponsor="'.$_SESSION["company"].'"'; //query to get all driver usernames within one company
$result = $conn->query($sql);
//$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){ //iterating throughout all the drivers
var_dump($row);
}
answered Nov 8 at 10:14
Tomas Macek
213
213
Checking it out in mysql workbench gives me all the rows, and using var dump gives me all of them too. That's part of what is stumping me.
– linksergey
Nov 8 at 10:20
OK - if you have all results try next thing - inside while display text of your sql inserts - are all correct?
– Tomas Macek
Nov 8 at 10:31
Still all correct. Every value in the row is called, the text for the sql insert is correct. It's just like insert I've done in other sections of my code. Is it something with the query call perhaps?
– linksergey
Nov 8 at 10:47
So in project.messages is same number of new records as number of $row? If the numbers match - isn't your problem in another part of your code?
– Tomas Macek
Nov 8 at 11:21
add a comment |
Checking it out in mysql workbench gives me all the rows, and using var dump gives me all of them too. That's part of what is stumping me.
– linksergey
Nov 8 at 10:20
OK - if you have all results try next thing - inside while display text of your sql inserts - are all correct?
– Tomas Macek
Nov 8 at 10:31
Still all correct. Every value in the row is called, the text for the sql insert is correct. It's just like insert I've done in other sections of my code. Is it something with the query call perhaps?
– linksergey
Nov 8 at 10:47
So in project.messages is same number of new records as number of $row? If the numbers match - isn't your problem in another part of your code?
– Tomas Macek
Nov 8 at 11:21
Checking it out in mysql workbench gives me all the rows, and using var dump gives me all of them too. That's part of what is stumping me.
– linksergey
Nov 8 at 10:20
Checking it out in mysql workbench gives me all the rows, and using var dump gives me all of them too. That's part of what is stumping me.
– linksergey
Nov 8 at 10:20
OK - if you have all results try next thing - inside while display text of your sql inserts - are all correct?
– Tomas Macek
Nov 8 at 10:31
OK - if you have all results try next thing - inside while display text of your sql inserts - are all correct?
– Tomas Macek
Nov 8 at 10:31
Still all correct. Every value in the row is called, the text for the sql insert is correct. It's just like insert I've done in other sections of my code. Is it something with the query call perhaps?
– linksergey
Nov 8 at 10:47
Still all correct. Every value in the row is called, the text for the sql insert is correct. It's just like insert I've done in other sections of my code. Is it something with the query call perhaps?
– linksergey
Nov 8 at 10:47
So in project.messages is same number of new records as number of $row? If the numbers match - isn't your problem in another part of your code?
– Tomas Macek
Nov 8 at 11:21
So in project.messages is same number of new records as number of $row? If the numbers match - isn't your problem in another part of your code?
– Tomas Macek
Nov 8 at 11:21
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205401%2fcorrectly-accessing-entire-row-for-database-insertion-with-mysqli-php%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Your code is a huge security risk!! You are vulnerable for sql-injection. Please read upon this and do the required steps to prevent it!!!!
– cramopy
Nov 8 at 10:05
1
Thank you for pointing it out, but I wasn't too concerned about it because it isn't for any commercial use. Just focused on learning more about mysqli and php, although I can definitely go back and fix it.
– linksergey
Nov 8 at 10:10
Well when you are just about learning and training it is as important as if it would be for any (non-) commercial use. Your goal shouldn't be fixing it only for this question, but learn how to do it the right way. This knowledge will be helpful all the way you go with sql.
– cramopy
Nov 8 at 10:12
What is the definition of the
messages
table - are there any unique constraints?– Nigel Ren
Nov 8 at 10:13
1
You're 100% right and I appreciate the advice. It's definitely something I will come back to, I was just stuck on this particular problem.
– linksergey
Nov 8 at 10:14