some questions

C++, C#, Java, PHP, ect...
Post Reply
Letterman
Posts: 3
Joined: Tue Mar 25, 2014 9:46 pm

some questions

Post by Letterman »

Hi, im new here :). I come here not only for gaming, but friend sayed this is realy great place for coding :). Sorry for my bad speak :). Okay, so i have question. For example i have db, where i submited am a list for example:

Code: Select all

$sql="INSERT INTO series (sname, sseason, syear, sseries)
so in other file i want to see this information in a simple table
sname info1, sseason info 1....
then new row
sname info2, sseason info 2....
then new row
etc... how to do this nasty thing?:D
EDIT: and other think, how to count information, for example i have 29 sname, and i want to see always how many sname is here? how to show it?:)
Letterman
Posts: 3
Joined: Tue Mar 25, 2014 9:46 pm

Re: some questions

Post by Letterman »

and next thing i need to do, for example if i trying to add same "snamelt" or "snameen" then i get error for example cant be same think. this is my all code:

Code: Select all

<html>
<head>
<title>Raimundas Pavilonis - filmai, serialai</title>
<meta charset="utf-8">
<meta name="Author" content="Raimundas Pavilonis">
<link rel="stylesheet" type="text/css" href="style.css">
</head> 
<body>
<?php
$con=mysqli_connect("localhost","rplist","rplist","rplist");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Neprisijungta prie MYSQL: " . mysqli_connect_error();
  }
$sql="INSERT INTO series (snamelt, snameen, sseason, syear, sseries)
VALUES
('$_POST[snamelt]','$_POST[snameen]','$_POST[sseason]','$_POST[syear]','$_POST[sseries]')";
if (!mysqli_query($con,$sql))
  {
  die('Klaida: ' . mysqli_error($con));
  }
echo "Serialo irašas pridetas sekmingai! <br> back to <a href=index.php>index</a>";


mysqli_close($con);
?>
</body>
</html>
Letterman
Posts: 3
Joined: Tue Mar 25, 2014 9:46 pm

Re: some questions

Post by Letterman »

nobody can help?:(
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: some questions

Post by Jackolantern »

Code: Select all

$sql="INSERT INTO series (sname, sseason, syear, sseries)
There is something wrong here. You have not completed the insert statement. It may look something like this:

Code: Select all

$sql = "INSERT INTO series (sname, sseason, syear, sseries) VALUES ('$sname', '$sseason', '$syear', '$sseries')";
Or whatever the variables are you are wanting to add to the database. The first set of ( ) is only saying what columns you will be inserting values into. You don't have to insert data into each column. If you don't insert data into some columns, they will get a NULL value. If you are inserting data into all columns, you can change your query to this:

Code: Select all

$sql = "INSERT INTO series VALUES ('$sname', '$sseason', '$syear', '$sseries')";
However, you can only do it this way if sname, sseason, syear, sseries are the only columns in the `series` table.

As for the issue of getting an error that you can't have an identical entry, that would be right. MySQL, and most other relational databases don't allow identical entries. The fix for this, if you need identical entries is to create an "id" column. Make it the primary key, and set it to auto-increment. Then on your insert queries, you will have to use the first form I showed above, because you won't ever be setting the "id" column directly. MySQL will automatically give it a number, and will add 1 to the next number for every new entry. That way you will never have 2 identical id columns, and no entries will ever be identical.

Let me know if you need more help :)

EDIT: Oh, I actually see you were using the second form when I looked at the whole script. I am not sure MySQL will like having a linebreak in the middle of a query. Put it all on one line. Also, you may need to get those $_POST[] values into regular variables before putting them into the SQL query string. PHP sometimes does not like associative array references (like $_POST['something']) inside of double-quote strings. I have been told the reason why this is before, but I forgot. So if you ever have a problem when you have array references like that built into a string, try either putting them into regular strings first, or concatenate the string, like this:

Code: Select all

$a = "This is ".$_POST['something']." and this is ".$_POST['somethingElse']."!!"; 
See what I mean. Being able to put variable references directly into double-quote strings is just a convenience. If it gives you trouble, there are other ways to do it.
The indelible lord of tl;dr
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: some questions

Post by Winawer »

Jackolantern wrote:I am not sure MySQL will like having a linebreak in the middle of a query.
Doesn't care.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: some questions

Post by Jackolantern »

Winawer wrote:
Jackolantern wrote:I am not sure MySQL will like having a linebreak in the middle of a query.
Doesn't care.
OK, I figured it may not, but I always just moved mine to the same line. I know SQL itself doesn't care, but didn't know if maybe PHP could pollute the string.

But anyway, I somehow missed the last part of the first post lol. I don't think it was a problem with the query at all, but how to show it. I think I saw the broken-off piece of the INSERT and then went on a tangent.
Letterman wrote:so in other file i want to see this information in a simple table
sname info1, sseason info 1....
then new row
sname info2, sseason info 2....
then new row
etc... how to do this nasty thing?:D
EDIT: and other think, how to count information, for example i have 29 sname, and i want to see always how many sname is here? how to show it?:)
It is actually pretty simple:

Code: Select all

//once you have a connection to the database

//get all of the records in the table
$result = mysqli_query($con, "SELECT * FROM series");

//open the table
echo '<table border="0">';

//make some headers
echo "<tr><th>namelt</th><th>nammen</th><th>season</th><th>year</th><th>series</th></tr>";

//loop through the results
while ($row = mysqli_fetch_assoc($result)) {
     echo "<tr>";
     echo "<td>".$row['snamelt']."</td>";
     echo "<td>".$row['snameen']."</tr>";
     echo "<td>".$row['sseason']."</td>";
     echo "<td>".$row['syear']."</td>";
     echo "<td>".$row['sseries']."</td>";
     //close the row
     echo "</tr>";
}

//now close the table
echo "</table>";

//close the db connection
mysqli_close($con); 
It may not be the exact way you want it formatted, but that should hopefully give you an idea of how to do it, and you can play with that until it is in a form you like. The other advice I gave about adding an idea to prevent duplicate entries still stands, despite the misunderstanding about what your initial problem was :)
The indelible lord of tl;dr
Post Reply

Return to “Coding”