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);
}









share|improve this question




















  • 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















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);
}









share|improve this question




















  • 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













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);
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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








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












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);
}





share|improve this answer





















  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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);
}





share|improve this answer





















  • 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

















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);
}





share|improve this answer





















  • 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















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);
}





share|improve this answer












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);
}






share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check